0

我的讨论在多态上属于项目、任务和子任务。当用户点击链接“完成讨论”时,讨论模型的布尔属性从假变为真。

我用下面的规范对此进行了 TDD 编辑,尽管布尔值确实发生了变化,但规范没有说,期望 false 为真。

这是下面的规格。它在第 73 行失败,说预期的错误为真。所以我的想法是我写错了规范。

  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

这是我的工厂(使用 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

请注意,我没有在工厂中添加“完成”属性,因为我在迁移中选择了默认值为 false。

编辑:这是当用户单击链接时将属性从 false 更改为 true 的操作(来自讨论控制器):

 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

你在第 37 行看到这个@discussion.finished 了吗?返回 true,当我使用 save_and_open_page 手动查看它时...

4

2 回答 2

1

好的,我从 IRC 的一个人那里得到了答案(名字很匆忙)。无论如何,规范没有通过的全部原因是因为我没有重新加载 discussion_for_subtask 实例。下面是有效的代码......干杯!

 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
于 2012-05-29T15:25:49.857 回答
0

您说“注意,我没有在工厂中添加 'finished' 属性,因为我在迁移中选择了默认值为 false。”

因此,在您的规范中,discussion_for_subtask.finished 是错误的。

该行discussion_for_subtask.finished.should == true失败

于 2012-05-29T13:47:37.490 回答