Rails ActiveRecord模型未在RSpec模型规范中更新

时间:2015-03-11 22:05:31

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord rspec

这让我疯了。我已经把它剥离到最低限度而不会失去上下文(我想!)

我要做的就是检查当我更新一个值并将其保存到数据库时,该值已保存。我想这样做是因为我需要编写一些其他代码,这些代码在before_save回调中有条件地阻止了这一点,并且在我确定它正常工作之前我无法测试它!

工厂和规格在下面,我确定它真的很蠢但是我无法理解......

FactoryGirl.define do

  factory :programme do
    name 'Trainee Programme'
  end

  factory :membership do
    programme
  end

  factory :specialty do
    sequence(:name) { |n| "Specialty #{n}" }
  end

  factory :user do
    sequence(:email) { |n| "factorygirl-user-#{n}@remailer.org" }
    password 'password'
    password_confirmation 'password'
    factory :trainee, class: User do
      sequence(:email) { |n| "factorygirl-trainee-#{n}@remailer.org" }
      name 'Factory Girl Trainee'
      after(:create) do |user|
        FactoryGirl.create(:membership, user: user, start_date: 1.day.ago)
      end
    end
  end

end

describe Membership do

  let(:trainee) { FactoryGirl.create(:trainee) }

  it 'sets specialty' do
    puts trainee.current_membership.inspect
    trainee.current_membership.specialty = specialty
    puts trainee.current_membership.inspect
    trainee.current_membership.save!
    puts trainee.current_membership.inspect
    expect(trainee.current_membership.specialty).to eq(specialty)      
  end

end

规范失败,因为期望值为零。当我运行代码时,我得到的调试输出是:

#<Membership id: 11, user_id: 11, programme_id: 11, start_date: "2015-03-10", end_date: nil, created_at: "2015-03-11 22:02:51", updated_at: "2015-03-11 22:02:51", options: {}, specialty_id: nil, membership_type_id: nil>
#<Membership id: 11, user_id: 11, programme_id: 11, start_date: "2015-03-10", end_date: nil, created_at: "2015-03-11 22:02:51", updated_at: "2015-03-11 22:02:51", options: {}, specialty_id: nil, membership_type_id: nil>
#<Membership id: 11, user_id: 11, programme_id: 11, start_date: "2015-03-10", end_date: nil, created_at: "2015-03-11 22:02:51", updated_at: "2015-03-11 22:02:51", options: {}, specialty_id: nil, membership_type_id: nil>

所以它好像专业的分配从未发生过?

3 个答案:

答案 0 :(得分:7)

尝试重新加载trainee,例如

expect(trainee.reload.current_membership.specialty).to eq(specialty)

答案 1 :(得分:1)

感谢BroiState和Mori给我一些指示,我能够确定它与持久性有关(特别是我的一个对象方法不尊重它!)

trainee.current_membership的代码如下:

def current_membership
  return unless memberships.current.any?
  memberships.current.first
end

在会员资格中使用这些相关范围...

scope :started, -> { self.where("#{table_name}.#{_start_field}::TIMESTAMP < '#{Time.now}'") }
scope :not_ended, -> { self.where("#{table_name}.#{_end_field} IS NULL OR #{table_name}.#{_end_field}::TIMESTAMP > '#{Time.now}'") }
scope :current, -> { self.started.not_ended }

因此,每次对trainee.current_membership的调用都会给我一个新的“当前&#39;会员记录

通过明确使用相同的对象,规范传递正常,即:

it 'sets specialty' do
  membership = trainee.current_membership
  membership.specialty = specialty
  membership.save!
  expect(membership.specialty).to eq(specialty.reload)      
end

答案 2 :(得分:0)

我能想到的唯一原因是speciality是一个新的非持久记录。由于membership belongs_to :specialitymembership已经保留,因此在分配和保存时都不会保存关联的对象。简而言之,确保在创建关联之前保存speciality