Rspec错误:未定义的方法'更改'为end.should更改(Model,:count)

时间:2011-02-10 19:45:51

标签: ruby-on-rails rspec nomethoderror

这是在完成Ruby on Rails教程后的后续内容。

我花了很长时间尝试混合搭配宝石来寻找解决方案,因为我真的不知道Rspec如何识别'变化'。

错误:'/spec/requests/athletes_spec.rb:20:in阻止(3级)':未定义方法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

我喜欢TDD的概念,但似乎有一半时间我试图让测试环境工作。谁能告诉我哪里出错了?

进一步编辑:

这是我的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 个答案:

答案 0 :(得分:1)

您可以尝试更改:

should_not change(Athlete, :count)

为:

should_not change { Athlete.count }

答案 1 :(得分:1)

首先,您需要使用it关键字提供示例。将内部describe更改为it

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

其次,在请求规范中,您需要指定URI而不是控制器操作,因此它应该读取类似的内容(取决于您的路由):

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