1

美好的一天,我从

 ActiveRecord::RecordNotFound:
   Couldn't find User without an ID

我的模型

 has_many :objects, class_name: 'OrderObject', dependent: :destroy
  belongs_to :user
  belongs_to :tariff

  validates :client, :phone, :tariff_id, :days, :user_id, presence: true

规格

 before do
    user = FactoryGirl.create(:user)
    FactoryGirl.create(:order, user_id: user.id)
  end
      context "validations" do
       it { should validate_presence_of :client  }
       it { should validate_presence_of :phone }
       it { should validate_presence_of :tariff_id }
       it { should validate_presence_of :days }
      end

      it { should have_many(:objects) } 
      it { should belong_to(:tariff) }
      it { should belong_to(:user) }

工厂

factory :order do
    client "MyString"
    phone "MyString"
    tariff_id 1
    days 1
    # advt_payed_day 1
    # firm_payed_day 1
    user_id 1
  end

更新 1

changed to
  before(:all) do
   user = FactoryGirl.create(:user )
   puts  user.id 
   order = FactoryGirl.create(:order, user_id: user.id ) 
   puts order.id

  end

输出

Order
45
32
  should have many objects
  should belong to tariff
  should belong to user
  validations
    should require client to be set (FAILED - 1)
    should require phone to be set (FAILED - 2)
    should require tariff_id to be set (FAILED - 3)
    should require days to be set (FAILED - 4)
    should require user_id to be set (FAILED - 5)

所以订单和用户被创建......

正如Rubyman建议的那样,更新 2 ,我改变了几件事:

in spec
  before(:all) do
   user = FactoryGirl.create(:user )
   #puts  user.id 
   order = FactoryGirl.create(:order, user_id: user.id ) 
   puts order 
   puts order.user_id

  end

在工厂

  factory :order do
    client "MyString"
    phone "MyString"
    tariff_id 1
    days 1
    # user_id 1
    association :user, factory: :user
  end

输出是:

Order
#<Order:0x00000005a866a0>
46
  should have many objects
  should belong to tariff
  should belong to user
  validations
    should require client to be set (FAILED - 1)
    should require phone to be set (FAILED - 2)
    should require tariff_id to be set (FAILED - 3)
    should require days to be set (FAILED - 4)
    should require user_id to be set (FAILED - 5)

1) Order validations 
     Failure/Error: it { should validate_presence_of :client  }
     ActiveRecord::RecordNotFound:
       Couldn't find User without an ID
     # ./app/models/order.rb:35:in `user_is_not_admin?'
     # ./spec/models/order_spec.rb:14:in `block (3 levels) in <top (required)>'

阅读tdgs的 建议后更新 3是变化:在模型中没有变化:

validates :client, :phone, :tariff_id, :days, :user_id, presence: true

在规范中

describe Order do
  before(:each) do
   user = FactoryGirl.create(:user )
   #puts  user.id 
   order = FactoryGirl.create(:order, user_id: user.id ) 
   puts order 
   puts order.user_id
   puts order.tariff_id
   puts order.phone
   puts order.days
   puts order.client
   puts '*****'
   user = User.find(order.user_id)
   puts user.login

  end
  context "validations" do
   it { should validate_presence_of :client  }
   it { should validate_presence_of :phone }
   it { should validate_presence_of :tariff_id }
   it { should validate_presence_of :days }
   it { should validate_presence_of :user_id }
  end

  it { should have_many(:objects) } 
  it { should belong_to(:tariff) }
  it { should belong_to(:user) }
end

输出:

#<Order:0x00000006c10ce0>
161
101
MyString
1
MyString
*****
user__7
    should require days to be set (FAILED - 1)

据我所知,每个应该的输出都是有效的......

UPDATE N 应该在开头就写好了。我已经在控制台中运行(希望它能解决这个问题)

bundle exec rake db:migrate
bundle exec rake db:migrate:reset db:test:prepare
4

4 回答 4

1

首先,您的工厂定义没有正确定义关联。你应该有这样的东西:

FactoryGirl.define do
  factory :user do
    sequence(:username) {|n| "username_#{n}"}
    # more attributes here
  end

  factory :tariff do
   # attributes
  end

  factory :order do
    client "MyString"
    phone "MyString"
    tariff
    user
    days 1
  end
end

那么你的测试应该这样写:

 context "validations" do
   it { should validate_presence_of :client  }
   it { should validate_presence_of :phone }
   it { should validate_presence_of :tariff_id }
   it { should validate_presence_of :days }
  end

  it { should have_many(:objects) } 
  it { should belong_to(:tariff) }
  it { should belong_to(:user) }

您当前在before过滤器中拥有的所有代码现在都不相关。另请注意,before(:all)在运行测试时 using 可能会产生一些奇怪的影响,因为它们不在事务中运行。before(:each)另一方面确实如此。

于 2012-10-12T10:06:40.417 回答
0

试试这个

  before { 
   @user = FactoryGirl.create(:user, :email => "test.com", :password => "test123", ... )
   @order = FactoryGirl.create(:order, :user_id => @user.id ) 
  }

工厂

require 'factory_girl'
FactoryGirl.define do
 factory :order do
  client "MyString"
  ...
  ...
 end
end
于 2012-10-12T08:41:22.110 回答
0

检查如何与工厂女孩建立关联 https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

于 2012-10-12T09:15:14.167 回答
0

我离开了应该并重写了验证检查。这样它的工作原理:

 before(:each) do
   @user = FactoryGirl.create(:user )
   @order = FactoryGirl.create(:order, user_id: @user.id ) 

  end
   it 'absence of client isn\'t acceptable' do
      temp = @order.client
      @order.client = ''
      @order.should_not be_valid
      @order.client = temp
      @order.should be_valid
    end
于 2012-10-15T14:35:37.807 回答