我有一个Rails 3应用程序,我正在使用RSpec进行测试。我有一个使用外部类MustMock
作为
class FooController < ApplicationController
def myaction
mockme = MustMock.new
@foobar = mockme.do_something
end
end
如何在控制器规范中最好地模拟MustMock
的实例?
答案 0 :(得分:5)
describe FooController do
specify :myaction do
MustMock.should_receive(:new)
.and_return(stub :do_something => :something)
get :myaction
assigns[:foobar].should == :something
end
end
答案 1 :(得分:2)
你可以试试这个:
it "does something in myaction" do
my_stub = stub()
my_stub.should_receive(:do_something).once.and_return(:foobar)
MustMock.stub(:new => my_stub)
get :myaction
response.should be_success
end