Rspec“未定义的方法`[]'为nil:NilClass”在Rails 3.2.8中

时间:2013-11-27 10:20:35

标签: ruby-on-rails ruby-on-rails-3 rspec

当我在User模型中测试时,我有一个名为Stb的模型和另一个模型Stb,我必须创建一个User实例。 当我独立运行testfile spec / models / stb_spec.rb 时,所有测试都过去了。 我一起运行所有测试,如:

bundle exec rake db:test:prepare && bundle exec rspec spec/

发生坏事:

Failure/Error: user.save
     NoMethodError:
       undefined method `[]' for nil:NilClass

我的User模型是:

class User < ActiveRecord::Base
    acts_as_authentic { |c| }
    acts_as_paranoid
    attr_accessible :name, :login, :password, :password_confirmation, :registed_by

    has_many   :user_stbs, :dependent => :nullify
    has_many   :stbs, :through => :user_stbs
    has_many   :feedbacks

    def has_record(program_asset_id)
        UserAsset.where(:uap_open_id => self.uap_open_id, 
                        :asset_id => program_asset_id ).first
    end

    ......
end

spec / models / stb_spec.rb 是:

describe Stb do
    before :all do
        @stb = FactoryGirl.create(:stb)
        user = User.new(:name => "Name", :login => "ccttsdf343",
                        :password => "111111", :password_confirmation => "111111")
        user.save
    end
end

我的 Gemfile 是:

# source 'https://rubygems.org'
source 'http://ruby.taobao.org/'

gem 'rails', '3.2.8'

gem 'mysql2'
gem 'authlogic'
gem 'redis', '~> 3.0.1'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'compass-rails'
end

gem 'jquery-rails', '~> 2.1.4'

group :test do
  gem 'rspec-rails', '~> 2.10.0'
  gem 'pry'
  gem 'pry-remote'
  gem 'pry-stack_explorer'
  gem 'pry-debugger'
  gem 'factory_girl_rails'
  gem 'db_fixtures_dump'
  gem 'simplecov', '~> 0.8.1'
  gem 'syntax', '~> 1.0.0'
end

group :development do
  gem 'debugger'
  gem 'factory_girl_rails'
  gem 'rspec-rails', '~> 2.10.0'
  gem 'sqlite3'
  gem 'annotate', :git => 'git://github.com/jeremyolliver/annotate_models.git', :branch => 'rake_compatibility'
  gem 'turn', :require => false
  gem 'factory_girl_rails'
  gem 'rspec-rails', '~> 2.10.0'
end
gem 'capistrano'
gem 'rvm-capistrano'

gem "acts_as_paranoid", "~>0.4.0"  # for Rails 3.2
gem 'paperclip'
gem 'eventmachine'
gem 'json'
gem 'uuid'
gem 'whenever', :require => false
gem 'kaminari'  # paginator

gem 'dalli'
gem 'redis', '~> 3.0.1'
gem 'hiredis'
gem "x_runtime", :git => "https://github.com/charlescui/x_runtime.git"
gem "em-apn"
gem "em-http-request"
gem "async_sinatra"
gem "mongoid"
gem "mongoid-paperclip", :require => "mongoid_paperclip"
gem "bson_ext"
gem 'rack_logger'
gem 'rack-fiber_pool', :git => "git://github.com/alebsack/rack-fiber_pool.git"
gem 'sad', '>=1.5.18'
gem 'blather'
gem 'rest-client'
gem 'digest-crc'
gem 'virtus'
gem 'rainbows'

1 个答案:

答案 0 :(得分:0)

尝试更改之前:all to before:each