0

运行以下rspecgem 版本:

  * rspec (2.14.1)
  * rspec-core (2.14.7)
  * rspec-expectations (2.14.5)
  * rspec-mocks (2.14.6)
  * rspec-rails (2.14.1)

octokit 2.7.1

鉴于以下内容spec/support/octokit.rb

# This support package contains modules for octokit mocking

module OctokitHelper
  def mock_pull_requests_for_org org
     mock_org_repos(org).map do |repo| 
      mock_pull_requests repo 
    end.flatten
  end
  def mock_org_repos org
    @@repos ||= [
      double('Sawyer::Resource', name: 'repo 1'),
      double('Sawyer::Resource', name: 'repo 2')
    ]
    Octokit.stub(:org_repos) { |org=org| @@repos }
    @@repos
  end
  def mock_pull_requests repo, gh_handle=nil
    @@pulls ||= [
      double('Sawyer::Resource', title: 'pr 1'),
      double('Sawyer::Resource', title: 'pr 2')
    ]
    Octokit.stub(:pull_requests) { |repo| @@pulls }
    @@pulls 
  end
end

RSpec.configure do |config|
  config.include OctokitHelper 
end

每当我第一次尝试mock_org_repos从规范中调用时,@@repos.first.name就是repo 1. 第二次,@@repos.first.name抛出一个非常奇怪的错误:

  1) StudentsController GET #submissions renders pull requests template
     Failure/Error: get :submissions, student_id: 2
       Double "Sawyer::Resource" received unexpected message :name with (no args)

我使用的一个例子mock_org_repos

require 'spec_helper'

describe StudentsController do
  describe 'GET #submissions' do
    before do
      @user = FactoryGirl.create(:instructor)
      @user.stub(:is_instructor?) { true }
      @submissions = mock_pull_requests_for_org ENV['GA_ORG_NAME']
      controller.stub(:current_user) { @user }
      get :submissions, student_id: 2
    end
    it 'assigns @submissions' do
      assigns(:submissions).should == @submissions
    end
    it 'renders pull requests template' do
      response.should render_template 'submissions'
    end
  end
end
4

2 回答 2

0

非常感谢rsofaer借给我第二双眼睛来解决这个问题,我想指出现在非常非常明显的问题:

每次运行示例时,当通过类变量引用存根时,在双精度上存根的方法都会被“清除”。

是对 RSpec 如何在幕后工作的非常清晰的解释。

于 2014-03-01T21:26:21.030 回答
0

我的猜测是您创建mock_pull_requests的同名双打之一正在发送:name,但您可以通过确保您的双打具有唯一名称(即 的第一个参数double)来检查这一点。

于 2014-02-20T23:59:02.733 回答