我有一個使用RSpec測試的Rails 3應用程序。我已經使用外部類MustMock
作爲模擬Rails控制器規範中的外部類
class FooController < ApplicationController
def myaction
mockme = MustMock.new
@foobar = mockme.do_something
end
end
我怎樣才能最好地嘲笑的MustMock
實例在我的控制器規格控制器?
我有一個使用RSpec測試的Rails 3應用程序。我已經使用外部類MustMock
作爲模擬Rails控制器規範中的外部類
class FooController < ApplicationController
def myaction
mockme = MustMock.new
@foobar = mockme.do_something
end
end
我怎樣才能最好地嘲笑的MustMock
實例在我的控制器規格控制器?
describe FooController do
specify :myaction do
MustMock.should_receive(:new)
.and_return(stub :do_something => :something)
get :myaction
assigns[:foobar].should == :something
end
end
你可以試試這個:
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
+1,我喜歡這個塊的形式很多。我要複製你的風格! :) – iain 2012-01-07 03:24:43