【问题标题】:Cannot get stub verification of should_receive to work when stubbing a method on a stub instance在存根实例上存根方法时,无法对 should_receive 进行存根验证
【发布时间】:2013-01-14 21:17:47
【问题描述】:

我正在尝试对 Koala 包装的 facebook 图形 api 进行存根。我的目标是验证图表是否已使用给定的访问令牌进行了初始化,并调用了“me”方法。

我的 rspec 代码如下所示:

需要'spec_helper'

describe User do

  describe '.new_or_existing_facebook_user' do
    it 'should get the users info from facebook using the access token' do
      # SETUP
      access_token = '231231231321'
      # build stub of koala graph that expected get_object with 'me' to be called and return an object with an email
      stub_graph = stub(Koala::Facebook::API)
      stub_graph.stub(:get_object). with('me'). and_return({
        :email => 'jame1231231tl@yahoo.com'
      })
      # setup initializer to return that stub
      Koala::Facebook::API.stub(:new) .with(access_token). and_return(stub_graph)

      # TEST
      user = User.new_or_existing_facebook_user(access_token)

      # SHOULD
      stub_graph.should_receive(:get_object).with('me') 
    end
  end
end

型号代码如下:

class User < ActiveRecord::Base
  # attributes left out for demo
  class << self
    def new_or_existing_facebook_user(access_token)
      @graph = Koala::Facebook::API.new(access_token)
      @me = @graph.get_object('me')

      # rest of method left out for demo
    end
  end
end

运行测试时出现错误:

  1) User.new_or_existing_facebook_user should get the users info from facebook using the access token
     Failure/Error: stub_graph.should_receive(:get_object).with('me')
       (Stub Koala::Facebook::API).get_object("me")
           expected: 1 time
           received: 0 times
     # ./spec/models/user_spec.rb:21:in `block (3 levels) in <top (required)>'

我是如何错误地存根该方法的?

【问题讨论】:

    标签: ruby rspec mocking


    【解决方案1】:

    should_receive 需要在方法被调用之前。 Rspec 消息期望通过接管方法并监听它来工作,与存根非常相似。事实上,你可以把它放在你的存根上。

    在规范的其余部分完成后,期望将决定它是否成功。

    试试这个:

    describe User do
    
      describe '.new_or_existing_facebook_user' do
        it 'should get the users info from facebook using the access token' do
          # SETUP
          access_token = '231231231321'
          # build stub of koala graph that expected get_object with 'me' to be called and return an object with an email
          stub_graph = stub(Koala::Facebook::API)
    
          # SHOULD
          stub_graph.should_receive(:get_object).with('me').and_return({
            :email => 'jamesmyrtl@yahoo.com'
          })
    
          # setup initializer to return that stub
          Koala::Facebook::API.stub(:new).with(access_token).and_return(stub_graph)     
    
          # TEST
          user = User.new_or_existing_facebook_user(access_token)
        end
      end
    end
    

    【讨论】:

      【解决方案2】:

      首先,我不会使用stub,因为存根表明您很可能不关心对象的行为。即使它们实例化了相同的东西,您也应该使用 mock。这更清楚地表明您想测试它的行为。

      您的问题来自于您在测试后设置了期望值。您需要在测试前设置期望值才能注册。

      【讨论】:

      • 模拟和存根之间的确切区别是什么?是否有任何文件可以清楚地解释这一点?
      • 它调用的内容没有区别。他们都制作了一个 Rspec 模拟对象。测试中的存根表明你不关心对象的行为,你只想实现它的功能。 mock 表示您正在测试某物的行为。它只是让它更具可读性。
      • 参见martinfowler.com/articles/mocksArentStubs.html 基本上,当人们阅读您的测试时,存根的行为驱动测试意义不如模拟,很明显您在这里测试行为。
      • 可以在 rspec rdoc 页面中找到简短描述:rubydoc.info/gems/rspec-mocks/frames
      猜你喜欢
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多