首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shoulda和before_回调

Shoulda和before_回调
EN

Stack Overflow用户
提问于 2013-10-17 02:01:46
回答 2查看 1.5K关注 0票数 3

我有一个简单的模型:

代码语言:javascript
复制
class Category < ActiveRecord::Base
  belongs_to :board
  validates :name, presence: true, uniqueness: {scope: :board_id}
  validates :board, presence: true
  validates :display_order, presence: true, uniqueness: {scope: :board_id}

  before_save :set_display_order

  private

  def set_display_order
    last = self.board.categories.order("display_order DESC").last
    self.display_order = last.display_order + 100 if last
  end
end

当我添加这个before_save回调时,这些测试开始失败:

代码语言:javascript
复制
  it { should validate_uniqueness_of(:display_order).scoped_to(:board_id) }
  it { should validate_uniqueness_of(:name).scoped_to(:board_id) }

With error (如果是私有方法last = ...中的行):

代码语言:javascript
复制
 NoMethodError:
   undefined method `categories' for nil:NilClass

其他的测试应该可以正常工作:

代码语言:javascript
复制
  it { should validate_presence_of(:name) }
  it { should validate_presence_of(:board) }
  it { should belong_to :board }

你知道这里有什么问题吗?我尝试将before_save更改为before_validation,但仍然是一样的。

EN

回答 2

Stack Overflow用户

发布于 2013-10-17 02:23:34

因为应该在数据库中创建记录。Gem创建记录跳过验证

http://rubydoc.info/github/thoughtbot/shoulda-matchers/master/Shoulda/Matchers/ActiveModel#validate_uniqueness_of-instance_method Ensures that the model is invalid if the given attribute is not unique. It uses the first existing record or creates a new one if no record exists in the database. It simply uses ':validate => false' to get around validations, so it will probably fail if there are 'NOT NULL' constraints. In that case, you must create a record before calling 'validate_uniqueness_of'.

在您的例子中,created category是空的,它表示category.board # => nil,您从nil调用categories方法。

您应该自己创建一条记录,用于唯一性测试。

票数 2
EN

Stack Overflow用户

发布于 2015-09-11 01:02:41

绕过shoulda_matchers和AR回调的这种限制的一种方法是重新定义匹配器应该使用的测试主题。

示例:

代码语言:javascript
复制
# category_spec.rb

# assuming you're using factorygirl and have this setup correctly
let(:board) { FactoryGirl.create(:board, :with_many_categories) }
subject { FactoryGirl.build(:category, board: board) }

# your shoulda matcher validations here
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19410519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档