问题描述
我讨论过多态地属于项目,任务和子任务.当用户单击链接完成讨论"时,讨论模型的布尔属性将从false更改为true.
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.
我已经在下面的规范中对此进行了TDD-ed处理,尽管布尔值确实发生了变化,但规范却未能如愿以偿,期望false为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.
这是下面的规格.它在第73行失败,说期望false为真.所以我的想法是我写的规范是错误的.
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
这是我的工厂(使用FactoryGirl):
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
请注意,我没有在工厂中添加完成"属性,因为我在迁移中选择了默认值为false.
Note, I don't have 'finished' attribute added in the factory, as I have chosen in the migration that the default is false.
这是当用户单击链接时(从讨论"控制器中)将属性从false更改为true的操作:
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
您在第37行看到了这个@ discussion.finish吗?当我使用save_and_open_page手动查看它时,返回true.
You see this @discussion.finished on line 37? that returns true, when I use save_and_open_page to see it manually...
推荐答案
好吧,我从IRC的一个家伙那里得到了答案(冲的是名字). 无论如何,规范未通过的全部原因是因为我没有重新加载Discussion_for_subtask实例.以下是有效的代码...干杯!
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
这篇关于测试失败,即使正在测试的事物通过了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!