Running the following rspec
gem versions:
* rspec (2.14.1)
* rspec-core (2.14.7)
* rspec-expectations (2.14.5)
* rspec-mocks (2.14.6)
* rspec-rails (2.14.1)
And octokit 2.7.1
Given the following in 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
Whenever I attempt to call mock_org_repos
the first time from a spec, @@repos.first.name
is repo 1
. The second time around, @@repos.first.name
throws a really bizarre error:
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)
An example of my usage of 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
Many thanks to rsofaer whom had lent me his second pair of eyes to resolve this, I would like to point out the now very very obvious problem:
Methods stubbed on doubles are 'cleared' every time examples are run, when the stubs are referenced via class variables.
This is a very clear explanation of how RSpec works under the covers.