我的讨论多态地属于项目,任务和子任务。当用户点击链接'完成讨论'时,讨论模型的布尔属性从false变为true。
我已经使用下面的规范对此进行了TDD-ed,尽管布尔值确实发生了变化,但规范未能说明,预计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
注意,我没有在工厂中添加'finished'属性,因为我在迁移中选择了默认值为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手动查看时...
答案 0 :(得分: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
答案 1 :(得分:0)
你说&#34;注意,我没有完成&#39;在工厂中添加的属性,正如我在迁移中选择的那样,默认值为false。&#34;
所以在你的规范中,discussion_for_subtask.finished是假的。
行discussion_for_subtask.finished.should == true
失败