【问题标题】:Rspec: Why isn't my model valid in general?Rspec:为什么我的模型通常无效?
【发布时间】:2012-09-14 01:28:30
【问题描述】:

问题与我的工厂有关,但我会先展示我的 location_spec.rb

require 'spec_helper'

describe Location do

  before(:each) do
    @location = FactoryGirl.build(:location)
  end

  subject { @location }

  describe "should be valid in general" do
    it { should be_valid }
  end

  describe "when user_id is not present" do
    before { @location.user_id = nil }
    it { should_not be_valid }
  end

  describe "when owner_id is not present" do
    before { @location.owner_id = nil }
    it { should_not be_valid }
  end
end

这是我的 factories.rb

FactoryGirl.define do
  factory :user do
    username   'user1'
    email      'user@example.com'
    timezone   'Eastern Time (US & Canada)'
    password   'testing'
  end

  factory :owner do
    name    'Owner One'
    user
  end

  factory :location do
    name 'Location One'
    about 'About this location'
    website 'http://www.locationone.com/'
    phone_number '12 323-4234'
    street_address 'Shibuya, Tokyo, Japan'


    association :owner, strategy: :build
  end
end

Location.rb

class Location < ActiveRecord::Base
  attr_accessible :about, :name, :owner_id, :phone_number,:street_address, :website
  belongs_to :owner
  belongs_to :user
  validates_presence_of :owner_id
  validates_presence_of :user_id
end

这是我得到的错误:

Failures:

  1) Location should be valid in general
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
    #./spec/models/location_spec.rb:53:in `block (3 levels) in <top (required)>'

我收到此错误是因为我为我相信的位置错误地建立了关联。关联应该是用户和所有者已经保存,而不仅仅是已经分配给两者的位置。我的工厂是否走在正确的轨道上,还是其他原因?你怎么看?

提前谢谢你。

【问题讨论】:

  • 好吧,我不是专家,但这是正确的吗? association :owner, strategy: :build ?还是应该是association :owner, strategy :build
  • @Kiran 我只是按照指南所说的去做:github.com/thoughtbot/factory_girl/blob/master/…。或者至少尝试这样做。
  • 如果您在 Location 模型中包含验证,可能会有所帮助。

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


【解决方案1】:

strategy: :build 选项告诉 FactoryGirl 不要实际保存 FactoryGirl.createFactoryGirl.build 的关联模型(所有者)。我不认为这是你想要的。

尝试将与所有者的工厂行更改为 owner,如下所示:

factory :location do
  name 'Location One'
  about 'About this location'
  website 'http://www.locationone.com/'
  phone_number '12 323-4234'
  street_address 'Shibuya, Tokyo, Japan'

  owner
end

这样做是在实例化位置记录之前创建关联,以便位置有效。如果您不这样做,则对父(位置)的验证将失败,因为没有要分配的owner_id(尚未创建所有者)。

更新

您的location 工厂中似乎还缺少user 关联,它也会验证该关联。因此,也将其添加到您的工厂以使其通过:

factory :location do
  name 'Location One'
  about 'About this location'
  website 'http://www.locationone.com/'
  phone_number '12 323-4234'
  street_address 'Shibuya, Tokyo, Japan'

  owner
  user
end

我认为应该这样做。

【讨论】:

  • 同样的事情仍然发生。顺便说一句,这是否也确保了我与User 的关联?
  • 哦,可能是因为您缺少用户,该位置也可以验证。尝试在工厂中的owner 下添加user
  • 我之前这样做过,当我这样做时,User 尝试从 Owner 创建中创建两次,但它当然不能,因为电子邮件和用户名已经被占用。我不知道如何将OwnerLocationUser 一起分配而不摆脱Owner 工厂内部的user
  • 您应该在用户工厂中为电子邮件使用序列,这将避免唯一性问题。 documentation 有信息,但基本上你用sequence :email { |n| "user#{n}@example.com" } 创建序列,然后在工厂中将电子邮件保留为email,没有电子邮件地址值(factorygirl 将使用序列自动创建它。
  • 哦,好的,现在我明白发生了什么。所有者和位置具有相同的用户,但另一个块中的所有者也具有相同的用户。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多