【问题标题】:rspec association creation errorrspec 关联创建错误
【发布时间】:2013-08-12 14:33:09
【问题描述】:

我有一个模型item has_many ratings 和一个ratings belongs_to item ratings belongs_to user 我想强制创建项目的用户也对其进行评分。其他用户可以稍后对其进行评分。 item 和 user 在我的模型中没有关联。 我在我的 item_spec 中执行以下操作,这在下面的行 @item = Item.new(name: "Item1", 上给了我一个错误 no implicit conversion of Symbol into Integer。

class Item < ActiveRecord::Base
  has_many :ratings, dependent: :destroy, inverse_of: :item
  accepts_nested_attributes_for :ratings, :allow_destroy => true
  validates :name , :length => { minimum: 3 }
  validates :category , :length => { minimum: 3 }
  validates_presence_of :ratings
end

require 'spec_helper'

describe Item do
  before do
    @item =  Item.new(name: "Item1",
                      url: "www.item1.com",
                      full_address: "Item1Address",
                      city: "Item1City",
                      country: "Item1Country",
                      category: "Item1Type",
                      ratings_attributes:  {"rating" => "3", "comment" =>  "Ahh Good"} )
  end

我也在使用 FactoryGirl 做这样的事情

factory :item do
    before_create do |r|
      r.ratings<< FactoryGirl.build(:ratings, item: r )
    end
    name "Item1"
    url "www.Item1.com"
    full_address "Item1Address"
    city "Item1City"
    country "Item1Country"
    category "Item1Category"
  end  

  factory :ratings do
    rating      3
    comment     "Its not that bad"
    user
  end
end

这再次没有产生预期的结果。
谁能帮我解决这个问题。谢谢!

【问题讨论】:

  • 您会分享您的堆栈跟踪还是确认您的规范中的行位于跟踪的顶部?另外,如果有Item 模型中的initialize 定义,您会分享吗?
  • @PeterAlfvin 为 '` 中的错误 2) Item Failure/Error: @item = Item.new(name: "Item1", TypeError: no implicit conversion of Symbol into Integer # ./spec/models/item_spec.rb:5:in block (2 levels) 添加了请求堆栈跟踪的信息
  • 我假设在上面的代码中你已经将你的 Item 模型文件与你的规范文件连接起来,否则第 5 行没有意义。
  • 我很困惑的一件事是你有 has_many :ratings 这意味着存在一个 Rating (单数)类,但你也有一个 Ratings (复数)类和一个Item 的ratings 属性。你会分享你的Rating 和Ratings 模型吗?
  • class Ratings &lt; ActiveRecord::Base belongs_to :user belongs_to :item, inverse_of: :ratings default_scope -&gt; { order('created_at DESC') } validates :user_id, :presence =&gt; true validates_presence_of :item validates_numericality_of :rating, :greater_than_or_equal_to =&gt; 0 validates_numericality_of :rating, :less_than_or_equal_to =&gt; 5 end

标签: ruby-on-rails factory-bot rspec2


【解决方案1】:

工作代码,现在在测试某些关联顺序时遇到问题,但至少所需的功能可以正常工作。

factory :item do
    name "Item1"
    url "www.Item1.com"
    full_address "Item1Address"
    city "Item1City"
    country "Item1Country"
    category "Item1Category"
  end

  factory :ratings, :class => 'Ratings' do
    association :item, factory: :item, strategy: :build
    user
    rating      3
    comment     "Its not that bad"
  end

  factory :item_with_rating, parent: :item do
    ratings {[FactoryGirl.create(:ratings)]}
  end

这是规范文件

require 'spec_helper'

describe Item do
  before do
    @item =  FactoryGirl.create(:item_with_rating)
  end

  subject { @item }
  it { should respond_to(:name) }
  it { should respond_to(:url) }
  it { should respond_to(:full_address)}
  it { should respond_to(:city) }
  it { should respond_to(:country) }
  it { should respond_to(:category) }
  it { should respond_to(:ratings) }
  it { should_not respond_to(:type) }
  it { should_not respond_to(:user_id) }
  it { should be_valid }

项目的模型文件没有变化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多