【问题标题】:Ruby on Rails error when testing for validations测试验证时出现 Ruby on Rails 错误
【发布时间】:2014-10-11 14:10:53
【问题描述】:

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

我的宝石文件:

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)>'

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

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby rspec devise


    【解决方案1】:

    看起来您正在测试 Account,而这些字段实际上位于 User 模型上。在此处查看您的架构:https://github.com/nikeshashar/saas/blob/master/db/schema.rb

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多