2011-07-03 3 views
15

와 validate_uniqueness_of :RSpec에, 했어야, 내가 RSpec에 테스트 다음 한 범위와 잘못된 오류 메시지

describe Productlimit do 

    before(:each) do 
    @productlimit = Factory.create(:productlimit, :user => Factory.create(:user)) 
    end 

    subject { @productlimit } 

    ... 

    it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) } 
    ... 
end 

하지만 혼란 오류 다음 얻을 :

1) Productlimit 
    Failure/Error: it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) } 
     Expected errors to include "has already been taken" when price_cents is set to 9530, got errors: ["direction_down has already been taken (false)"] 

당신이 날 도와 줄 수 있습니까? 오류 메시지가 올바른 것 같아서 이것이 왜 작동하지 않는지 이해할 수 없습니까?

편집 :

이뿐만 아니라 다른 상황에서 너무 발생합니다

# product_spec.rb 
... 
it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") } 

# rake spec:models 
Failure/Error: it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") } 
    Expected errors to include "price_cents must be greater than 0 (0)" when price is set to "abcd", got errors: ["price_cents must be greater than 0 (0)"] 
+0

당신은 shoulda/rspec의 어떤 버전을 사용하고 있습니까? – nathanvda

+0

나는 shoulda-matchers (1.0.0.beta2)를 가지고 있지만, 현재 'shoulda'보석으로도 시험해 보았습니다. – Lichtamberg

+0

레일이 이미 시험하고있는 것을 시험하고있는 것처럼 보입니다. 모델에 validate_numericality가있는 경우 테스트에서 호출하는 이유는 무엇입니까? 그것은 불필요한 중복입니다. – corroded

답변

11

validate_uniqueness_of (: field)를 확인하려면 데이터베이스의 레코드가 유일해야 단일 제한을 검사해야합니다. 여기 있습니다 ....

before do 
    @country = FactoryGirl.create(:country, :id =>"a0000007-0000-0000-0000-000000000009",:currency_id => "a0000006-0000-0000-0000-000000000004",:merchant_id => "a0000001-0000-0000-0000-000000000002",:country_code => 'CAN', :name => 'Canada') 
end 

it { should validate_uniqueness_of(:country_code)} 

그것은 해결을 시도합니다 고유성의 유효성을 검사합니다.

+0

감사합니다. –

0

봅니다 test/fixtures/*를 제거하고 수동으로 (공장을 사용하지 않는) 고유성을 테스트하기 전에 개체를 만들려고 할 수 있습니다. 또한 시도해 보셨습니까 :

class MyModelTest < ActiveSupport::TestCase 
+0

rspec (테스트 폴더가 전혀 없음)로 테스트하고 수동으로 내 개체를 저장하려고 시도했습니다. 그러나 시험은 효과가 없습니다. – Lichtamberg

2

sepc_helper에 database_cleaner 설정이 있습니까? 다음 spec_helper.rb 에서

gem "database_cleaner" 

config.use_transactional_fixtures = false 

config.before(:suite) do 
    DatabaseCleaner.strategy = :truncation 
end 

config.before(:each) do 
    DatabaseCleaner.start 
end 

config.after(:each) do 
    DatabaseCleaner.clean 
end 

는 또한이 공장의 코드를 게시 할 수있는 RSpec.configure 블록에 다음을 추가 추가하지 않을 경우? 단지 더 선명한 그림을 얻기 위해서.

이 정보가 도움이되기를 바랍니다.

direction_down 속성에 고유성 확인이 있는지 확인하십시오.

+0

귀하의 답변을 주셔서 감사합니다,이 특정 테스트를 제거하고 수동으로 직접 테스트 ... 솔루션을 시도했지만 그 중 하나도 작동하지 않습니다 .. – Lichtamberg

0

price_cents의 고유성이 유효한지 테스트 할 때 : direction_down의 고유성에 대해 불평하고 있기 때문에 정규식에 대한 오류가 올바르지 않습니다.

모델의 코드에 validates_uniqueness_of :direction_down이 포함되어 있습니까? 그렇다면 메시지를 설명합니다.

3

Riche에 direction_down에 대한 고유성 유효성 검사에 대해 언급 한 내용을 추가하려면 ProductLimit 팩토리가 direction_down에 대한 값을 생성하는 방법을 의심해보십시오. 고유성 유효성 검증을 갖는 모든 속성에 대해 고유 한 값을 생성하는 것은 아 U니다.

또한 고유성 유효성 검사에 직면 한 첫 번째 문제는 유효성 검사가 "임의로"생성되는 팩터와 충돌하는 값을 갖지 않도록하기 전에 만들어진 첫 번째 개체 (사례의 경우)입니다.

Factory(:product_limit, :direction_down => "abc") 
it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down) } 

실패 할 가능성이, 사소한 예로 설명하기 잘못이 경우에 했어야 정합에 의해 구성되는 객체 direction_down 세트로 끝난다 "ABC"의 유일성 검증가 다음과 direction_down이.

0

첫 번째 경우 (예 : validate_uniquess_of)는 예기치 않은 충돌 후에 나에게 일어났습니다. 간단한 rake db:test:prepare가 수정되었습니다.

관련 문제