Ruby on rails 如何仅在使用rspec进行更新时测试模型对象的有效性?

Ruby on rails 如何仅在使用rspec进行更新时测试模型对象的有效性?,ruby-on-rails,ruby-on-rails-3,unit-testing,rspec,rspec2,Ruby On Rails,Ruby On Rails 3,Unit Testing,Rspec,Rspec2,我有一个这样的侧面模型 class Profile < ActiveRecord::Base attr_accessible :first_name, :last_name belongs_to :user validates :user, presence: true validates :first_name, presence: true, on: :update validates :last_name, presence: true, on: :update

我有一个这样的侧面模型

class Profile < ActiveRecord::Base
  attr_accessible :first_name, :last_name
  belongs_to :user
  validates :user, presence: true
  validates :first_name, presence: true, on: :update
  validates :last_name, presence: true, on: :update
end

不区分更新或创建操作,我在rspec文档中看不到任何与此相关的内容。当然,这是一个相当常见的测试。

在rspec中是有效的
,只需在模型上调用
有效?

profile = FactoryGirl.build :profile
这将为
Profile
构建一个新的模型实例,但不会将其提交到数据库。您可以将此
配置文件
用于创建测试。将
:first_name
设置为
nil
应该,调用
profile.should应该是有效的
应该通过

profile = FactoryGirl.create :profile

这将生成并将
配置文件的模型实例插入数据库中。您将使用此
配置文件进行更新测试。设置
:first_name
nil
应该并调用
profile.should.be\u valid
应该失败

好的,这其实很简单,我认为应该会发生。在我的模型中有一些回调(为了简单起见,我省略了这些回调)会干扰这一点。
profile = FactoryGirl.create :profile