首页 \ 问答 \ 如何测试double,mock或stub控制器端对象(How to test double, mock or stub controller side object)

如何测试double,mock或stub控制器端对象(How to test double, mock or stub controller side object)

我正在编写一个控制器测试,我想验证Controller1的call_dashboard_function。

class Controller1 < ApplicationController 
  before_action :init_dashb

  def call_dashboard_function
     @dashb.do_something!
  end 

  def init_dashb
    @dashb ||= Dashboard.new(current_user)
  end 
end 

class Dashboard
  #... this is a facade class
end 

实际测试:

  test "should do something" do 
    sign_in @user
    patch :call_dashboard_function
  end 

问题是我不想测试仪表板类,因为我有一个单独的测试。 所以我想模拟,存根等等这类行为

如果我从外面访问,那将很容易。 但我无法从控制器外部看到仪表板类。


I'm writing a controller test, and I wanted to verify Controller1's call_dashboard_function.

class Controller1 < ApplicationController 
  before_action :init_dashb

  def call_dashboard_function
     @dashb.do_something!
  end 

  def init_dashb
    @dashb ||= Dashboard.new(current_user)
  end 
end 

class Dashboard
  #... this is a facade class
end 

Actual test:

  test "should do something" do 
    sign_in @user
    patch :call_dashboard_function
  end 

The problem is I don't want to test the dashboard class, since I have a separated test for it. So I'd like to mock, stub, etc this class behavior

That would be easy if I had access from outside. But I can't see dashboard class from outside the controller.


原文:https://stackoverflow.com/questions/40708887
更新时间:2023-05-16 17:05

最满意答案

是的,一切都发生在AuthorizeRequest期间,因为这允许在ProcessRequest之前将请求重写为静态文件(如果缓存)。


Yes, everything happens during AuthorizeRequest, as this permits the request to be rewritten to a static file (if cached) prior to ProcessRequest.

相关问答

更多
  • 是的,一切都发生在AuthorizeRequest期间,因为这允许在ProcessRequest之前将请求重写为静态文件(如果缓存)。 Yes, everything happens during AuthorizeRequest, as this permits the request to be rewritten to a static file (if cached) prior to ProcessRequest.
  • 避免不惜一切代价直接使用System.Drawing - 它充满了内存泄漏,并发问题和编码错误 。 ImageResizer为您处理和避免这些问题,并提供Watermarking插件以及SampleOverlay示例插件,以向您展示如果Watermark无法满足您的需求,可以安全地创建自己的插件。 如果我想要获得Bitmap结果,您可以将typeof(Bitmap)作为'destination'参数传递给ImageJob,并从.Result属性访问Bitmap实例。 其他后果: 除非将其置于finally( ...
  • 您应该检查事件委托类型。 来自msdn : 公共事件EventHandler AuthorizeRequest 因此, AuthorizeRequest委托是EventHandler 。 来自msdn : public delegate void EventHandler(Object sender,EventArgs e) AuthorizeRequest参数: 对象发送者 EventArgs e You should check event delegate type. From msdn: publi ...
  • 使用ImageResizer.Net,您可以为宽度指定(?w = 96)和height =(&h = 96)并决定缩放模型,例如(&mode = max) 但是,您无法指定最大尺寸,您可以尝试使用文件格式(格式= gif | png | jpg)或/和质量(q = 50(0..100))来查找一系列图像的最佳设置 有关更多信息,请访问http://imageresizing.net/docs/basics 谢谢 巴蒂尔 With ImageResizer.Net you can specify with ( ...
  • 好吧,看起来我找到了自己的解决方案。 这是问题所在: return (virtualPath.StartsWith("assets", StringComparison.OrdinalIgnoreCase) StartsWith将始终返回false,因为virtualPath将以我的主机名开头。 切换到Contains()语句可以完美地工作。 很棒的插件,顺便说一下! Well, looks like I found my own solution. Here's the problem: return ...
  • 是的,disckcache文件夹是自定义为/ images /而不是/ imagecache / 通过升级到ImageResizer 3.4.1(从3.3或3.4)和随附的更新的DiskCache插件修复了此问题。 Yes, the disckcache folder was customised to /images/ instead of /imagecache/ This issue was fixed by upgrading to ImageResizer 3.4.1 (from 3.3 or 3 ...
  • ImageResizer不支持.eps,但Ghostscript支持。这是PdfRenderer的基础。 我建议修改/分支PdfRenderer以引入.eps支持。 我们没有任何计划正式支持这一点,但我们肯定会将添加了该功能的pull请求合并到PdfRenderer。 我们的大部分努力(re:pdf)将在PdfiumRenderer上进行 ,后者具有更好的特性。 ImageResizer doesn't support .eps, but Ghostscript does, which underlies ...
  • 不,不支持此功能。 您可以创建一个插件,可以很容易地在背景上平铺图像 - 但是从BuilderExtension继承并覆盖PostRenderBackground 。 为什么不使用CSS呢? No, this feature isn't supported. You could create a plugin that tiles an image on the background pretty easily, though - inherit from BuilderExtension and over ...
  • 您在提供代码之前使用过logo.InputStream (可能用于其他工作)。 在using语句之前,您可以再次读取图像或将建议的异常放在下面的代码中 logo.InputStream.Seek(0, SeekOrigin.Begin); You have used logo.InputStream before provided code (maybe for other job). You can read image again or as exception suggested put below ...
  • 扫描仪仍然添加exif旋转数据。 请分享您的诊断页面,以便我们了解您在做什么。 通常你会添加&autorate=true或全局设置它: 请参见: 如何在ImageResizer中使用autorotate插件 ImageResizer旋转问题 为什么ImageResizer自动旋转图像垂直拍摄并显示水平 Scanners still add exif rotation data. Please sha ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)