我有一个可以属于项目、任务和子任务的讨论(多态资源)。我在测试以下创建操作时遇到了麻烦:
26 def create
27 @discussion = @parent.discussions.build(params[:discussion])
28 @discussion.user_id = current_user.id
29 if @discussion.save
30 current_user.discussions.push(@discussion)
31 redirect_to [@parent, @discussion], :notice => 'Discussion started'
32 else
33 render 'new'
34 flash.now[:alert] = 'Unable to start discussion'
35 end
36 end
这些是在创建操作之前发生的过滤器之前(这些找到必要的东西)
63 private
64
65 def find_cached_parent_or_from_something_id
66 @parent || find_parent_from_something_id # this does a bit of caching
67 end
68
69 def find_parent_from_something_id
70 @parent = nil
71 params.each do |name, value|
72 if name =~ /(.+)_id$/
73 @parent = name.humanize.constantize.find(value)
74 end
75 end
76 @parent
77 end
78
79 def find_project_from_parent_belonging_project
80 @project = @parent.belonging_project
81 unless current_user.projects.include?(@project)
82 redirect_to projects_path
83 flash[:alert] = msg_page_not_found
84 end
85 end
如您所见,我可以找到父项和所需的所有变量。现在这就是我的RSpec 控制器对创建操作的测试的样子:它们都通过了:
104 it "can create discussion on project using valid attributes" do
105 lambda do
106 post :create, :project_id => project,
107 :discussion => valid_attributes
108 flash[:notice].should == 'Discussion started'
109 end.should change(Discussion, :count).by(1)
110 end
111
112 it "can create discussion on task using valid attributes" do
113 lambda do
114 post :create, :task_id => task,
115 :discussion => valid_attributes
116 flash[:notice].should == 'Discussion started'
117 end.should change(Discussion, :count).by(1)
118 end
119
120 it "can create discussion on subtask using valid attributes" do
121 lambda do
122 post :create, :subtask_id => subtask,
123 :discussion => valid_attributes
124 flash[:notice].should == 'Discussion started'
125 end.should change(Discussion, :count).by(1)
126 end
但是,我更喜欢尽可能 DRY 测试所有父母的讨论创建,类似于下一个测试的内容。
104 ['project', 'task', 'subtask'].each do |parent|
105 it "can create discussion on #{parent} using valid attributes" do
106 lambda do
107 post :create, :parent_id => parent,
108 :discussion => valid_attributes
109 flash[:notice].should == 'Discussion started'
110 end.should change(Discussion, :count).by(1)
111 end
112 end
如何正确编写此测试?
我正在使用 FactoryGirl 顺便说一句。上面的这个测试返回 > NameError: uninitialized constant Parent
另外,如果有更好的方法/做法,请务必纠正我:)
编辑:我已经使用下面接受的答案解决了这个问题,并且偶然发现了另一个非常相似的问题,我再次不知道......
142 it "can access show action for discussion for #{parent}" do
143 get :show, :"#{parent}_id" => self.send(parent),
144 :id => "disucussion_by_another_user_for_#{parent}"
146 response.should be_successful
147 end
这个 :id 参数写错了……你能帮我写正确吗?(这个讨论_by_another_user_for_#{parent} 是我定义的3个不同的工厂......)
这是我的工厂的样子:
16 let!(:discussion_by_another_user_for_project) { FactoryGirl.create(:discussion,
17 :user => another_user,
18 :discussionable => project) }
19 let!(:discussion_by_another_user_for_task) { FactoryGirl.create(:discussion,
20 :user => another_user,
21 :discussionable => task) }
22 let!(:discussion_by_another_user_for_subtask) { FactoryGirl.create(:discussion,
23 :user => another_user,
24 :discussionable => subtask) }