2012-01-07 53 views
2

我有一个使用RSpec测试的Rails 3应用程序。我已经使用外部类MustMock作为模拟Rails控制器规范中的外部类

class FooController < ApplicationController 
    def myaction 
    mockme = MustMock.new 
    @foobar = mockme.do_something 
    end 
end 

我怎样才能最好地嘲笑的MustMock实例在我的控制器规格控制器?

回答

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 
+0

+1,我喜欢这个块的形式很多。我要复制你的风格! :) – iain 2012-01-07 03:24:43

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 
+0

一个小问题 - 我倾向于把'response.should be_success'在shared_example,因为这将是如此普遍希望,对于一个页。 YMMV – iain 2012-01-07 03:22:31

+1

是的,有几种方法可以改进测试。我怀疑OP的控制者行动更为复杂,他/她将问题归结为最小可证明的行动。 – Finbarr 2012-01-07 03:24:03

+0

完全同意,我确实意味着次要:) – iain 2012-01-07 03:26:08

相关问题