It's easy to get into a test implementation, but it's a good candidate for testing behavior.
Ask yourself what you do before you actually do the check. Then do yourself a test, which, given certain inputs, you will get certain outputs, if and only if yours before the test works.
As a simple example, suppose you have a user model with a simple name
attribute, and you have before_validation that just does:
name = "Auto-generated name." if name.blank?
Then you can write a test for it as follows:
it "gets an auto-generated name if none is specified." do user = User.create! user.name.should eq "Auto-generated name." end it "does not get an auto-generated name if one is specified." do user = User.create!(:name => "Test Name") user.name.should eq "Test Name" end
So, as you can see, this test checks the validation, but it does not need to be publicly available, because you are just checking its behavior. Does the same apply to the unique?
method unique?
. Give it specific inputs and test the behavior without worrying about which particular method was used or any other implementation details.
source share