I have a Discussion (polymorphic resource) that can belong to Project, Task and Subtask. I have troubles testing the following create action:
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
These are before filters that happen before create action (these find the necessary things)
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
As you can see, I can find the parent and all the variables needed. Now this is how my RSpec controller test for the create action looks like: They all pass:
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
But, I’d prefer to test creation of discussion on all parents as DRY as possible, something along the lines of the next test.
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
How to write this test properly?
I am using FactoryGirl btw. This test above returns > NameError: uninitialized constant Parent
Also, if there is better way/practice of doing this, by all means, correct me 🙂
EDIT: I have solved the problem using the accepted answer below and have stumbled upon another one, very similar, that I again don’t know…
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
This :id parameter is written wrong… Can you please help me write it properly? (this discussion_by_another_user_for_#{parent} are 3 different factories I defined…)
This is how my factories look like:
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) }
I think you were almost there:
Hope this helps.
[EDIT] Your extra question: I am not quite sure how you believe handing down a mere string would actually create an object. You create an object with FactoryGirl as follows:
So, if you take that into account, your test becomes:
Of course, you could inline the parameter
discussion_by_another_user, but I think this is a bit more readable. Hope this helps.[EDIT] After clarified question: this is actually completely identical that what you asked before.
You build a string, but the string is actually a method you want to call, and as before, you
just write:
HTH