I have a bunch of tests that check to make sure I'm validating various properties of a model. All of a sudden, I started having tests fail because the validations seemed to be adding the same error twice.
'User creation should require domain names to be unique' FAILEDThis error only occured when my full suite of tests ran. If I ran the unit test by itself (or even if I just ran only the model tests by themselves), it didn't happen. Unfortunately I didn't notice whatever it was I did to introduce this error, so I couldn't reverse it.
expected 1 error on :domain, got 2
Several other people have encountered this problem, so I know it's because my tests are leaking state in some way. Somehow I am doing something during my tests that rspec isn't able to clean up. Usually it's because the tests are doing something weird with extra require or load statements, which causes multiple copies of the class to be loaded. Removing these statements usually works.
I had no such statements and so spent a while trying to sort this out. On a whim, I added a require statement to the top of the failing User model spec, and that fixed it:
require 'user'I wish I had more time to investigate, because it makes me thing I don't know enough about Rails autoloading behavior, or Ruby's loading behavior.
If you're running into this same problem, I found these mailing lists threads to be useful:
3 comments:
I found something like this when I required the model twice due to slightly different paths. By default ruby keeps track of what you've required based on the path, and if the path differs at all, then it will require the same file two or more times. I've found that consistently using File.expand_path to normalize the path you're requiring helps alot. I wish ruby did this automatically.
Thanks Mike! This got me on the right path :)
"Do you think Oz could give me courage?" asked the Cowardly Lion.
"Just as easily as he could give me brains," said the Scarecrow.
"Or give me a heart," said the Tin Woodman.
"Or send me back to Kansas," said Dorothy.
Nike shoes
MBT
supra footwear
famous footwear
shoe dept
nike air max
Post a Comment