Why adding '=' to targeted method?

18 views
Skip to first unread message

Samir Sabri

unread,
Jan 31, 2013, 5:41:56 AM1/31/13
to factor...@googlegroups.com

I am using this factory to create Quizes for my test:

  factory :quiz_with_two_choices_first_correct, :class => Quiz do |i|
    quiz_type Quiz.SINGLE_ANSWER_CHOICE
    weight 1

    i.after_create do |quiz|
      quiz.quiz_choices = [FactoryGirl.create(:quiz_choice, :body=>'Quiz Choice 1', :is_correct=>true, :position=>1),
                           FactoryGirl.create(:quiz_choice, :body=>'Quiz Choice 2', :is_correct=>false, :position=>2)]
    end
  end

In my Quiz model I have:

  after_create { |record|

    if !current_unit.nil? then
      if current_unit_type.eql? FinalExam.to_s then
        current_unit.total_weights=
            current_unit.total_weights+ record.weight
        current_unit.save
      end
    end

  }

But when I try to test, I get this error:

Failure/Error: quiz= FactoryGirl.create(:quiz_with_two_choices)
     NoMethodError:
       undefined method `after_create=' for #<Quiz:0xb50075c>

Here is my test:

describe "When a final question is created" do

  it "can't be deleted if any student is enrolled to it" do
    quiz= FactoryGirl.create(:quiz_with_two_choices)
    final_question = FinalExamQuestion.create(:quiz_id=>quiz.id)
    quiz_count_before_try_to_destroy_quiz= Quiz.all.count
    quiz.destroy
    Quiz.all.count.should == quiz_count_before_try_to_destroy_quiz
  end
  it "can be deleted if there isn't any student enrolled to it" do
    quiz= FactoryGirl.create(:quiz_with_two_choices)
    quiz_count_before_try_to_destroy_quiz= Quiz.all.count
    quiz.destroy
    Quiz.all.count.should_not == quiz_count_before_try_to_destroy_quiz
  end
end

So,  what could be wrong? why it says undefined method `after_create=' while I am calling after_create  i.after_create do |quiz|

Any idea? how to solve this?


Joe Ferris

unread,
Jan 31, 2013, 10:07:57 AM1/31/13
to factor...@googlegroups.com
Hey Samir,

You don't need the block parameter to your factory definition ("i"), and the method is now just "after" and takes a parameter. The modified factory would look like this:

    factory :quiz_with_two_choices_first_correct, :class => Quiz do
      quiz_type Quiz.SINGLE_ANSWER_CHOICE
      weight 1

      after :create do |quiz|
        quiz.quiz_choices = [FactoryGirl.create(:quiz_choice, :body=>'Quiz Choice 1', :is_correct=>true, :position=>1),
                             FactoryGirl.create(:quiz_choice, :body=>'Quiz Choice 2', :is_correct=>false, :position=>2)]
      end
    end

-Joe
--
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training
 
You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en
---
You received this message because you are subscribed to the Google Groups "factory_girl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to factory_girl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Samir Sabri

unread,
Jan 31, 2013, 1:55:33 PM1/31/13
to factor...@googlegroups.com
Thanks, it worked now.
Reply all
Reply to author
Forward
0 new messages