> I'm not failiar with that factory notation

It's a syntax of FactoryGirl that you can enable with:
RSpec.configure do |config|
  config.include Factory::Syntax::Methods
end


> but if your factory is like FactoryGirl and that :participation factory 
> template uses a :user factory template internally for the association, then 
> if it is like FactoryGirl it will do a create on the user even if you are 
> doing build on the outer template. This is a bit of a stab in the dark since 
> I'm not sure what you are using for factories and what is in your templates 
> but I have been stung like this with factorygirl before. If your 
> participation had a user and that user was already persisted then your 
> callback won't go off…

Yeah, I was bitten by this as well. So I set explicitly the "build" strategy 
most of the time on associations.

But in this particular case I set the association set to nil explicitly.
It is also demonstrated from the debug console (Pry) that I provided at the end 
of the message (participation.user returns nil). 

So there should not be an issue with the factory.
I must be doing something wrong with the AR callbacks.

> 
> On 31/10/2011, at 1:59 PM, Dmytrii Nagirniak wrote:
> 
>> Hi,
>> 
>> I must have some stupid mistake or something.
>> Just can't seem to pass the spec.
>> 
>> Could you guys please help me find where the evil has hidden in the details?
>> 
>> 
>> describe Participation do
>>   describe "invitation by email", :focus do
>>     let(:participation) { build :participation } # Factory
>>     let(:email)         { participation.email }
>> 
>>     it "should send an invitation" do
>>       # This one is failing
>>       binding.pry # The code below is executed here
>>       participation.should_receive(:invite_user!)
>>       participation.save!
>>     end
>>     
>>     context "when user already exists" do
>>       let!(:existing) { create :user, :email => email }
>>       it "should not send an invitation" do
>>         participation.should_not_receive(:invite_user!)
>>         participation.save!
>>       end
>> 
>>     end
>>   end
>> end
>> 
>> 
>> 
>> Can't see anything wrong with the callbacks:
>> 
>> # Check the before_validation callback options:
>> participation.user # nil
>> participation.valid? # true
>> participation.user # User{id: nil}
>> 
>> # Check the before_create callback options:
>> participation.user_exists? # false
>> participation.mark_for_invitation # true
>> 
>> # Check the after_create callback options:
>> participation.marked_for_invitation? # true
>> 
>> # After all this I expect the "invite_user!" to be called:
>> participation.stub(:invite_user!) { puts "Doesn't get called :(" }
>> participation.save! # => true, Nothing is printed, which is consistent with 
>> the spec
>> participation.user_id # => 11, so the user has been saved
>> 
>> 
>> The actual implementation is:
>> 
>> 
>> class Participation < ActiveRecord::Base
>>   attr_accessor :email
>> 
>>   belongs_to :user
>>   validates  :email, :email => true, :on => :create, :if => :using_email?
>> 
>>   before_validation :set_user_by_email,   :if     => :using_email?, :on => 
>> :create
>>   before_create     :mark_for_invitation, :unless => :user_exists?
>>   after_create      :invite_user!,        :if     => :marked_for_invitation?
>> 
>> 
>>   def using_email?
>>     email.present?
>>   end
>> 
>>   def user_exists?
>>     user.present? and user.persisted?
>>   end
>> 
>>   def set_user_by_email
>>     self.user = User.find_by_email(email)
>>     self.user ||= User.new(email: email).tap do |u|
>>       u.status = :invited
>>     end
>>   end
>> 
>>   def mark_for_invitation
>>     @invite_user = true
>>     true # make sure not cancelling the callback chain
>>   end
>> 
>>   def marked_for_invitation?
>>     !!@invite_user
>>   end
>> 
>>   def invite_user!
>>     # TODO: Send the invitation email or something
>>   end
>> end
>> 
>> 
>> Can't spot the problem.
>> 
>> 
>> Thanks in advance,
>> Dima
>> http://www.ApproachE.com
>> 
>> 
>> 
>> 
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby or Rails Oceania" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to 
>> [email protected].
>> For more options, visit this group at 
>> http://groups.google.com/group/rails-oceania?hl=en.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to