Test failing, even though the thing it is testing

2019-09-16 12:31发布

问题:

I have discussions that polymorphically belong to project, task, and subtask. When user clicks on link 'finish discussion', the boolean attribute of discussion model changes from false to true.

I have TDD-ed this with the spec below, and allthough the boolean value indeed does change, the spec is failing saying, expected false to be true.

here is the spec below. It fails on the line 73, saying expected false to be true. So my thinking is I have wrote the spec wrong.

  5   let!(:user) { FactoryGirl.create(:confirmed_user) }
  6   let!(:project) { FactoryGirl.create(:project) }
  7   let!(:task) { FactoryGirl.create(:task, :project => project) }
  8   let!(:subtask) { FactoryGirl.create(:subtask, :task => task) }
  9   let!(:discussion_for_subtask) { FactoryGirl.create(:discussion,
 10                                                      :user => user,
 11                                                      :discussionable => subtask) }
 15 
 16   before do
 17     sign_in_as!(user)
 18     user.projects << project
 19   end

 68     it 'should be able to finish the discussion if they started it/are admin' do
 69       visit subtask_discussions_path(subtask)
 70       click_link "#{discussion_for_subtask.name}"
 71       current_path.should == subtask_discussion_path(subtask, discussion_for_subtask)
 72       click_link 'Finish discussion'  
 73       discussion_for_subtask.finished.should == true
 74       current_path.should == subtask_discussions_path(subtask)
 75     end

And here are my factories ( using FactoryGirl ):

  1 FactoryGirl.define do      
 16 
 17   # USER                   
 18   factory :user do         
 19 
 20     sequence(:name) do |n|
 21       "Name#{n}"           
 22     end  
 23     sequence(:email) do |n|
 24       "FactoryEmailNumber#{n}@example.com"
 25     end                    
 26     password 'secret'      
 27     password_confirmation 'secret'  
 28 
 29     factory :confirmed_user do
 30       after_create do |user|
 31         user.confirm!
 32       end
 33     end
 34 
 35   end
105   # SUBTASK
106   factory :subtask do
107     sequence(:name) do |n|
108       "Subtask - #{n}"
109     end
110     task
111   end
112 
113   # DISCUSSION
114   factory :discussion do
115     sequence(:name) do |n|
116       "Discussion No#{n}"
117     end
118     description "Description for the discussion"
119   end

Note, I don't have 'finished' attribute added in the factory, as I have chosen in the migration that the default is false.

Edit: this is the action that changes attribute from false to true when user clicks the link(from discussions controller):

 33   def finish
 34     if current_user.discussions.include?(@discussion)
 35       @discussion.update_attribute(:finished, true)
 36       redirect_to polymorphic_path([@parent, Discussion])
 37       flash[:notice] = "it worked #{@discussion.finished}"
 38     else
 39       flash[:alert] = 'You must be an admin to do that'
 40     end
 41   end

You see this @discussion.finished on line 37? that returns true, when I use save_and_open_page to see it manually...

回答1:

Ok, I got the answer from a guy over at IRC (rushed is the name). Anyway, the whole reason for the spec not passing is because I didn't reload discussion_for_subtask instance. Below is the code that works... Cheers!

 68     it 'should be able to finish the discussion if they started it/are admin' do
 69       visit subtask_discussions_path(subtask)
 70       click_link "#{discussion_for_subtask.name}"
 71       current_path.should == subtask_discussion_path(subtask, discussion_for_subtask)
 72       click_link 'Finish discussion'  
 73       # the following reload is needed so the instance knows about the change you put it through
 74       discussion_for_subtask.reload   
 75       discussion_for_subtask.finished.should == true
 76       current_path.should == subtask_discussions_path(subtask)
 77     end


回答2:

You said "Note, I don't have 'finished' attribute added in the factory, as I have chosen in the migration that the default is false."

So in your spec discussion_for_subtask.finished is false.

That the row discussion_for_subtask.finished.should == true fails