gpt4 book ai didi

ruby-on-rails - rails + rspec : Staying DRY when testing validations

转载 作者:行者123 更新时间:2023-12-04 22:08:52 26 4
gpt4 key购买 nike

好的说我有以下模型:

class Country < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :code
end

我正在为这些验证做 rspec 单元测试。它们看起来像这样:
  it "should be invalid without a name" do
country = Country.new(@valid_attributes.except(:name))
country.should_not be_valid
country.errors.on(:name).should == "can't be blank"
country.name = @valid_attributes[:name]
country.should be_valid
end

it "should be invalid without a code" do
country = Country.new(@valid_attributes.except(:code))
country.should_not be_valid
country.errors.on(:code).should == "can't be blank"
country.code = @valid_attributes[:code]
country.should be_valid
end

这看起来不太干。是否有任何 gem 或插件可以自动执行此类操作?
我想得到一些东西:
  it "should be invalid without a name" do
test_presence_validation :name
end

it "should be invalid without a code" do
test_presence_validation :code
end

最佳答案

值得一提的是:http://github.com/carlosbrando/remarkable

在你可以做之后

it { should validate_presence_of :name }

关于ruby-on-rails - rails + rspec : Staying DRY when testing validations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2469608/

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