I am using FactoryGirl to test a model that is being extended with a Concern. A simplified version is below:
class SampleUser < ActiveRecord::Base
attr_accessible :username, :email
If I create a SampleUser with factory girl create, then the before_validation hooks in the concern are run. If I use 'build' or 'build_stubbed' though, then my before_validation hooks are somehow being skipped. Is that desired behavior? I couldn't find it in the documentation.
The text was updated successfully, but these errors were encountered:
Hey @brycesenz, it doesn't call the before_validation callbacks on the model because FactoryGirl doesn't call #valid? on the model after it builds it. Here's the doc explaining how FG plays with ActiveRecord; basically when you call build or build_stubbed, it's just instantiating the object and stopping (so, no #save!, no #valid?, etc.)
To have the before_validation callbacks run, you'll need to check validity or save and then assert that whitespace is removed. Hope that clears things up!
To clarify, if you're explicitly testing your before_validation callbacks, you should probably just call #valid? on the instance manually (since it's a lot more explicit as to what's going on instead of hiding it in a trait), but I just wanted to demonstrate that you could technically achieve a validated object with FG. Hope this helps!