gpt4 book ai didi

ruby-on-rails - RSpec 测试不会通过。验证域名的唯一性

转载 作者:数据小太阳 更新时间:2023-10-29 08:52:17 24 4
gpt4 key购买 nike

我正在使用 ruby​​ 1.9.2 和 rails 3.2.2。

我有一个“领域”模型(domain.rb):

class Domain < ActiveRecord::Base
attr_accessible :url
belongs_to :user

VALID_DOMAIN_REGEX = /^[a-z0-9\-\.]+\.[a-z]{2,}$/i
validates :url, presence:true,
format: { with: VALID_DOMAIN_REGEX },
uniqueness: { case_sensitive: false }

end

以及断言重复域无效的测试:

require 'spec_helper'

describe Domain do

before do
@domain = FactoryGirl.create(:domain)
end

subject { @domain }

describe "when domain url is already taken" do
before do
domain_with_same_url = @domain.dup
domain_with_same_url.url = @domain.url.upcase
domain_with_same_url.save
end
it { should_not be_valid }
end
end

测试一直失败:

1) Domain when domain url is already taken Failure/Error: it { should_not be_valid } expected valid? to return false, got true # ./spec/models/domain_spec.rb:31:in `block (3 levels) in '

最佳答案

@domain 已创建、验证并保存。

domain_with_same_url 是新记录,应该是无效的。但你没有检查它。

尝试

domain_with_same_url = FactoryGirl.create(:domain, :url => @domain.url.upcase)
domain_with_same_url.should_not be_valid

关于ruby-on-rails - RSpec 测试不会通过。验证域名的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9896793/

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