我使用ruby 1.9.2和rails 3.2.2。RSpec測試不會通過。使用域名驗證唯一性
我有一個 '域' 模型(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 '
感謝您的幫助!現在意識到我在概念上缺少一個大項目:)應該幫助我繼續這個測試的東西。 – jessem 2012-03-27 21:38:08