【发布时间】:2014-03-27 15:18:11
【问题描述】:
使用 Mocha,我试图模拟一个调用模块方法的控制器方法。 这是一个集成测试。
例子:
class Controller < ApplicationController
def method1
response = Module1.method2(...
到目前为止我的步骤:
- 将 mocha 添加到 gemfile 中
- 在我的最底部添加了
require 'mocha/mini_test'test_helper.rb -
在向我的控制器发送帖子之前,在我的集成测试中尝试了此代码:
Module1.stub(:method2).returns(:true) post "controller/method1" -
得到了这个错误:
NoMethodError: undefined method 'stub' for Module1:Module
是否可以存根method2?
编辑:所以主要的解决方法是该方法是“存根”而不是“存根”。不过,我仍然无法模拟这个该死的模块。
编辑:Rails 和 MiniTest 只是在我将其存根之后才调用模块方法。 Rails 是否有可能覆盖我的存根?
class Test < ActionDispatch::IntegrationTest
test "test do
Module1.stubs(:method2).returns(:true)
post "controller/method1"
此测试导致 method2 内部出错,因为没有传入参数。测试表现得好像该方法没有被存根。
【问题讨论】:
标签: ruby-on-rails mocking integration-testing mocha.js