【问题标题】:RoR, RSpec - problem with validation testingRoR,RSpec - 验证测试的问题
【发布时间】:2010-08-31 10:07:15
【问题描述】:

通过 rspec(我正在使用 rspec-1.3.0、rspec-rails-1.3.2 gems)生成器 (ruby script/generate rspec_model suggestion section_id:integer user_id:integer subject:string content:text state:string type:string) 我创建了模型和模型规范并运行 rake db:migrate and rake:test:prepare

之后我开始研究我的模型规范:

require 'spec_helper'

describe Suggestion do
  before(:each) do
    @valid_attributes = {
      :section_id => 1,
      :user_id => 1,
      :subject => 'Inappropriate title',
      :content => 'The title of this section is inappropriate.',
      :state => 'new',
      :type => 'flag'
    }
  end

  it "should create a new instance given valid attributes" do
    Suggestion.create!(@valid_attributes)
  end

  it "should reject empty section_id attribute" do 
    empty_section_id_suggestion = Suggestion.new(@valid_attributes.merge(:section_id => ""))
    empty_section_id_suggestion.should_not be_valid
  end

  ...

除了第一个 "should create a new instance given valid attributes" 测试我创建了 6 个测试,基本上每个建议模型的测试属性为空 - 几乎与 "should reject empty section_id attribute" 示例完全相同。

当我运行测试时,我得到了 6 个失败的测试,这很好。第一次测试"should create a new instance given valid attributes" 通过。

现在,当我使用建议模型并添加 validates_presence_of :all 时,我收到以下与第一次测试相关的错误消息:

ActiveRecord::RecordInvalid in 'Suggestion should create a new instance given valid attributes'
Validation failed: All can't be blank
./spec/models/suggestion_spec.rb:16:

当我尝试单独运行测试 (validates_presence_of :attribute) 时,所有测试都通过了,只有 :type 属性我再次收到类似的错误消息:

ActiveRecord::RecordInvalid in 'Suggestion should create a new instance given valid attributes'
Validation failed: Type can't be blank
./spec/models/suggestion_spec.rb:16:

我以前没有遇到过这个问题(有多个相似的模型和它们的规格都通过了)。看起来它的 :type 属性有问题(它说它不能为空),即使我通过@valid_attributes 将值传递给它。我尝试谷歌搜索,但没有找到类似的问题/解决方案。

这里是 :type 属性的测试

it "should reject empty type attribute" do 
        empty_type_suggestion = Suggestion.new(@valid_attributes.merge(:type => ""))
        empty_type_suggestion.should_not be_valid
  end

请检查一下,让我知道我在这里做错了什么。

非常感谢您的帮助

彼得

【问题讨论】:

    标签: ruby-on-rails validation activerecord rspec


    【解决方案1】:

    在您的模型中,您不能只说 validate :all 因为 :all 不是列名。

    class Suggestion < AR::Base
      validates_pressence_of :subject, :content
    end
    

    没有理由验证 id 列的存在,但如果你愿意,我想你可以。

    api 文档: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_presence_of

    【讨论】:

    • 嗨,杰德,感谢您的回答。你说的对。我不知道为什么我认为可以通过 :all 一次性验证所有模型属性。这解决了一半的问题:ActiveRecord::RecordInvalid in 'Suggestion should create a new instance given valid attributes' 验证失败:All can't be blank ./spec/models/suggestion_spec.rb:16: It still get error message on :type 属性
    • 类型是保留字。你必须在你的装置和代码中改变它。尝试将其更改为类别之类的内容,这样它就会显示为 Suggestion.category
    【解决方案2】:

    所以最后我找到了与:type属性相关的问题的答案:

    http://www.gyrotechie.com/2008/09/activerecord-does-not-like-attributes-called-type/

    问题在于 type 是从 ActiveRecord 继承的类的保留字段名称

    我通过迁移重命名了字段名并修改了所有相关文件,现在一切正常。

    【讨论】:

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