Rails RR Framework:多次调用instance_of

时间:2009-05-18 13:43:44

标签: ruby-on-rails rspec bdd

我想使用RR为我的控制器编写RSpec。

我写了以下代码:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe RegistrationController do

    it "should work" do 
        #deploy and approve are member functions
        stub.instance_of(Registration).approve { true }
        stub.instance_of(Registration).deploy { true }
        post :register
    end 
end

但是,当仍然调用原始批准方法时,RR存根仅部署方法。

我应该使用什么语法来存储所有Registration类实例的方法调用?

更新 我用[摩卡]

取得了理想的结果
Registration.any_instance.stubs(:deploy).returns(true)
Registration.any_instance.stubs(:approve).returns(true)

2 个答案:

答案 0 :(得分:1)

您所描述的行为实际上是一个错误:

http://github.com/btakita/rr/issues#issue/17

答案 1 :(得分:-1)

据我所知,RSpec模拟不允许你这样做。您确定,您需要存根所有实例吗?我通常遵循这种模式:

describe RegistrationController do
    before(:each) do
       @registration = mock_model(Registration, :approve => true, :deploy => true)
       Registration.stub!(:find => @registration)
       # now each call to Registration.find will return my mocked object
    end

    it "should work" do 
      post :register
      reponse.should be_success
    end

    it "should call approve" do
      @registration.should_receive(:approve).once.and_return(true)
      post :register
    end

    # etc 
end

通过对您控制的Registration类的find方法进行存根,可以在规范中返回哪个对象。