gpt4 book ai didi

ruby-on-rails-4 - Rspec 3 - 如何在模型测试中处理 before_validation

转载 作者:行者123 更新时间:2023-12-05 00:24:34 25 4
gpt4 key购买 nike

在我的模型中,我有一个 before_validation 方法:

class Post < ActiveRecord::Base
before_validation :generate_url
...

private

def generate_url
return unless self.url.blank?
year = self.published_at.class == ActiveSupport::TimeWithZone ? self.published_at.year : DateTime.now.year
self.url = "#{year}/#{self.title.parameterize}"
end

rspec/models/post.spec.rb

测试 w rspec-rails (3.1.0) 和 factory_girl_rails(4.4.1),我得到了一个 spec/models/post_spec.rb 来验证 Post 模型和一个 spec/factories/post.rb

由于 before_validation 验证测试失败
 Post should require title to be set
Failure/Error: it { is_expected.to validate_presence_of(:title) }
NoMethodError:
undefined method `parameterize' for nil:NilClass
# ./app/models/post.rb:69:in `generate_url'
# ./spec/models/post_spec.rb:20:in `block (2 levels) in <top (required)>'

在 generate_url 方法中使用调试语句,我得到 self 为 nil ...所以似乎工厂帖子没有被考虑在内

规范/模型/post_spec.rb
require 'rails_helper'

RSpec.describe Post, :type => :model do
it { is_expected.to validate_presence_of(:title) }
it { is_expected.to ensure_length_of(:title).is_at_least(5) }
it { is_expected.to validate_presence_of(:content) }
it { is_expected.to ensure_length_of(:content).is_at_least(10) }
it { is_expected.to validate_presence_of(:url) }
it { is_expected.to validate_uniqueness_of(:url) }
it { is_expected.to validate_presence_of(:published_at) }
end

规范/工厂/post.rb
FactoryGirl.define do
factory :post do
user_id 1
title "myTitle"
content "myContent."
published false
published_at "2014-09-16 08:46:38"
end
end
# note : attribute url is generated with before_validation

最佳答案

可以跳过 before_validation :

RSpec.describe Post, :type => :model do

before{ Post.skip_callback(:validation, :before, :generate_url) }

it { is_expected.to validate_presence_of(:user_id) }
it { is_expected.to validate_presence_of(:title) }
it { is_expected.to ensure_length_of(:title).is_at_least(5) }
it { is_expected.to validate_presence_of(:content) }
it { is_expected.to ensure_length_of(:content).is_at_least(10) }
it { is_expected.to validate_presence_of(:url) }
it { is_expected.to validate_uniqueness_of(:url) }
it { is_expected.to validate_presence_of(:published_at) }
end

您还可以提供一个块,以便仅跳过该块的验证,如下所示:
RSpec.describe Post, :type => :model do

Post.skip_callback(:validation, :before, :generate_url) do
it { is_expected.to validate_presence_of(:user_id) }
it { is_expected.to validate_presence_of(:title) }
it { is_expected.to ensure_length_of(:title).is_at_least(5) }
it { is_expected.to validate_presence_of(:content) }
it { is_expected.to ensure_length_of(:content).is_at_least(10) }
it { is_expected.to validate_presence_of(:url) }
it { is_expected.to validate_uniqueness_of(:url) }
it { is_expected.to validate_presence_of(:published_at) }
end
end

关于ruby-on-rails-4 - Rspec 3 - 如何在模型测试中处理 before_validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25969955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com