测试验证时,Ruby on Rails错误

时间:2014-10-11 14:10:54

标签: ruby-on-rails ruby rspec devise

所以我正在关注这个tutorial,在第5集中,我按照指示更新了Rails 4.1.6的语法,我正在使用新的Gems。我的代码在Github

我的Gemfile:

source 'https://rubygems.org'

gem 'rails', '4.1.6'
gem 'pg'

gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'spring',        group: :development
gem 'bootstrap-sass'
gem 'autoprefixer-rails'
gem 'simple_form'
gem 'devise'

group :development, :test do 
  gem 'guard'
  gem 'guard-livereload'
  gem 'guard-rspec'
  gem 'rspec-rails'
  gem 'rspec-collection_matchers'
  gem 'capybara'
  gem 'factory_girl_rails'
  gem 'launchy'
  gem 'database_cleaner'
  gem 'shoulda-matchers'
  gem 'warden'
end

我的spec / models / user_spec.rb具有以下内容:

require 'rails_helper'

describe Account do 

  describe 'validations' do 
    it { should validate_presence_of (:name)}
    it { should validate_presence_of (:email)}
    it { should validate_presence_of (:password)}
  end

  describe 'associations' do 
  end

end

我的app / models / user.rb有:

class User < ActiveRecord::Base
  devise :database_authenticatable, :recoverable, :rememberable, :validatable

  validates :name, presence: true

end

运行RSpec时出现以下错误:

Account
  validations
    should require name to be set (FAILED - 1)
    should require email to be set (FAILED - 2)
    should require password to be set (FAILED - 3)

Failures:

  1) Account validations should require name to be set
     Failure/Error: it { should validate_presence_of (:name)}
     NoMethodError:
       undefined method `name=' for #<Account:0x00000108092050>
     # ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'

  2) Account validations should require email to be set
     Failure/Error: it { should validate_presence_of (:email)}
     NoMethodError:
       undefined method `email=' for #<Account:0x00000108088640>
     # ./spec/models/user_spec.rb:7:in `block (3 levels) in <top (required)>'

  3) Account validations should require password to be set
     Failure/Error: it { should validate_presence_of (:password)}
     NoMethodError:
       undefined method `password=' for #<Account:0x00000108079af0>
     # ./spec/models/user_spec.rb:8:in `block (3 levels) in <top (required)>'

现在已经坚持了一段时间,无法弄清楚我还需要检查什么。任何想法都会有很大的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

当这些字段实际位于用户模型上时,您似乎正在测试帐户。在此处查看您的架构:https://github.com/nikeshashar/saas/blob/master/db/schema.rb

作为建议,将这些验证检查放在用户中,然后在rspec帐户测试中,测试该帐户是否拥有所有者。因此,account.owner应具有这些属性并与用户进行相同的验证。