Skip to content

Instantly share code, notes, and snippets.

@sevenseacat
Last active December 18, 2015 12:59
Show Gist options
  • Select an option

  • Save sevenseacat/5787196 to your computer and use it in GitHub Desktop.

Select an option

Save sevenseacat/5787196 to your computer and use it in GitHub Desktop.
Inconsistencies in using rspec's `expect` syntax. Does it take an argument or a block? In which circumstances does it take each? Why can't we all just get along!?
describe SeriesController do
describe 'GET "create"' do
subject { post :create, series: params }
context 'valid parameters provided' do
let(:params) { FactoryGirl.attributes_for :series }
# Using expect(subject) here leads to an error:
# NoMethodError: undefined method `call' for #<ActionController::TestResponse:0x007f03c4c3d470>
specify { expect { subject }.to change(Series, :count).by(1) }
# Using expect { subject } here leads to an error:
# Expected response to be a <redirect>, but was <200>
specify { expect(subject).to redirect_to series_path(assigns(:series)) }
end
end
end
@asiniy
Copy link

asiniy commented Nov 1, 2013

Hello,

my chunk of code that works (maybe it will help you?)

describe Frontend::UsersController, type: :controller do
  describe 'POST "create"' do
    subject { post :create, user: { email: email } }

    context 'with valid email' do
      let(:email) { FactoryGirl.attributes_for(:user)[:email] }
      specify { expect { should change(User, :count).by(1) } }
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment