You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
Disclaimer: I am new to RSpec and trying to build tests on an existing app using the some of the examples in this book. The below is replicating a test similar to the one on page 35 of your PDF.
I have a 'Price' Model which has only one required column 'id' which is unique and set to AUTO_INCREMENT at the database (MySQL) level.
# spec/models/price_spec.rbrequire'spec_helper'describePricedoit"has a valid factory"doexpect(create(:price)).tobe_valid# this passesendit"is invalid when id is duplicate"docreate(:price,id: 29595276)expect(build(:price,id: 29595276)).tohave(1).errors_on(:id)# this failsendend
The RSpec error I get is as follows
Price
has a valid factory
is invalid when id is duplicate (FAILED - 1)
Failures:
1) Price is invalid when id is duplicate
Failure/Error: expect(build(:price, id: 29595276)).to have(1).errors_on(:id) # this passes
expected 1 errors on :id, got 0
# ./spec/models/price_spec.rb:13:in `block (2 levels) in <top (required)>'
Finished in 0.1942 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/models/price_spec.rb:11 # Price is invalid when id is duplicate
Am I doing something wrong or testing something that I shouldn't be testing with RSpec (since the database will take care of enforcing uniqueness)? I was expecting that the 'DuplicateRecord' error from MySQL would be captured and reported as an error resulting in my test passing - right?
Any pointers would be very helpful.
Cheers
The text was updated successfully, but these errors were encountered:
Testing a factory doesn't make sense. It's only to help you to create proper objects easier.
Also trying to test uniqueness this way doesn't make any sense. Probably you'll want to use the shoulda matcher like this:
it { should validate_uniqueness_of(:entity_attribute) }
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Disclaimer: I am new to RSpec and trying to build tests on an existing app using the some of the examples in this book. The below is replicating a test similar to the one on page 35 of your PDF.
I have a 'Price' Model which has only one required column 'id' which is unique and set to AUTO_INCREMENT at the database (MySQL) level.
My factory is setup as below
My spec is as follows
The RSpec error I get is as follows
Am I doing something wrong or testing something that I shouldn't be testing with RSpec (since the database will take care of enforcing uniqueness)? I was expecting that the 'DuplicateRecord' error from MySQL would be captured and reported as an error resulting in my test passing - right?
Any pointers would be very helpful.
Cheers
The text was updated successfully, but these errors were encountered: