【发布时间】:2014-12-25 07:18:37
【问题描述】:
我已经习惯了 RSpec,您可以在其中使用 evalueted lazily 的块来存根方法实现。
现在我正在处理一个使用 Test::Unit 和 mocha 作为模拟库的项目的拉取请求。
我需要能够做一些类似于 rspec 示例的事情,即将方法实现替换为取决于对象状态的动态实现,因此我不能使用 mocha #returns 方法提供的静态调用。
有什么方法可以使用 mocha 获得相同的功能,我找不到任何关于此的文档?
我需要实现类似的东西(RSpec 2.14 语法)
class SomeController < ApplicationController
before_filter :authenticate
def authenticate
# original method I need to replace
end
def some_other_method
:bar
end
end
describe SomeController do
before do
controller.stub :authenticate do
redirect_to root_path if some_other_method == :foo
end
end
it 'should test something' do
controller.stub(some_other_method: :foo)
get :index
response.should redirect_to root_path
end
it 'should test something else' do
get :index
response.should be_successful
end
end
【问题讨论】:
标签: ruby-on-rails ruby testing rspec stubbing