2014-02-20 81 views
0

運行以下rspec寶石版本:好生奇怪紅寶石 '|| =' 和RSpec雙打行爲

* 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.namerepo 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 

回答

0

非常感謝rsofaer誰借給我他的第二雙眼睛來解決這個問題,我想指出現在非常非常明顯的問題:

方法對雙打存根被「清除」每次當通過類變量引用存根時,會運行示例。

This是RSpec如何在封面下工作的非常清晰的解釋。

0

我的猜測是,你在mock_pull_requests與創建的雙打之一相同的名字發送:name,但你可以檢查確保你的雙打有獨特的名字(即double的第一個參數)。