Rails RR 框架:多次调用instance_of

发布于 2024-07-21 00:07:43 字数 729 浏览 4 评论 0原文

我想使用 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

但是,当仍然调用原始 approve 方法时,RR 仅存根 deploy 方法。

我应该使用什么语法来对 Registration 类的所有实例的两个方法调用进行存根?

更新: 我用[Mocha]达到了预期的结果

Registration.any_instance.stubs(:deploy).returns(true)
Registration.any_instance.stubs(:approve).returns(true)

I would like write RSpec for my controller using RR.

I wrote following code:

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

However RR stubs only deploy method when still calls original approve method.

What syntax should I use to stub both method calls for all instances of Registration class?

UPDATE:
I achivied desired result with [Mocha]

Registration.any_instance.stubs(:deploy).returns(true)
Registration.any_instance.stubs(:approve).returns(true)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2024-07-28 00:07:44

据我所知,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 方法,在规范中返回什么对象。

as far as I know, the RSpec mocks don't allow you to do that. Are you sure, that you need to stub all instances? I usually follow this pattern:

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

By stubbing the find method of the Registration class you control, what object gets returned in the spec.

巴黎盛开的樱花 2024-07-28 00:07:43

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

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

It would appear the behavior you describe is actually a bug:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文