Rspec error: undefined method 'change' for end.should change(Model, :count)

2.5k Views Asked by At

This is a follow on after completing the Ruby on Rails tutorial.

I've spent quite a while trying to mix and match gems to find a solution since I really have no idea how Rspec is having trouble identifying 'change'.

Error: /spec/requests/athletes_spec.rb:20:inblock (3 levels) in ': undefined method change' for #<Class:0x00000100e715e8> (NoMethodError)

require 'spec_helper'

describe "Athletes" do

  before(:each) do
    @attr = { :gender => "male",
        :age => 20,
      :height => 120,
      :weight => 100,
      :skill => 2
    }
    @user = Factory(:user)
  end

  describe "Creating Athletes" do

    describe "with the correct user should work" do
      lambda do
        post :create, :athlete => @attr
      end.should_not change(Athlete, :count)

    end

  end
end

I love the concept of TDD but it seems like half the time I'm trying to get the testing environment to work. Can anyone tell me where I'm going wrong?

FURTHER EDIT:

Here's my Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.4'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'
gem "will_paginate", "3.0.pre2"
gem 'jquery-rails', '>= 0.2.6'



group :development do
  gem 'rspec-rails', '2.5.0'
  gem 'annotate-models'
end

group :test do
  gem 'factory_girl_rails'
  gem 'rspec', '2.5.0'
  gem 'webrat'
end
2

There are 2 best solutions below

1
On BEST ANSWER

First, you need to supply an example using the it keyword. Change the inner describe to it:

describe "Creating Athletes" do
  it "with the correct user should work" do

Second, in a request spec you need to specify the URI instead of the controller action, so it should read something like this (depending on your routes):

post '/athletes', :athlete => @attr

0
On

You could try changing:

should_not change(Athlete, :count)

to:

should_not change { Athlete.count }