プロジェクト、タスク、およびサブタスクに属することができるディスカッション (ポリモーフィック リソース) があります。次の作成アクションのテストに問題があります。
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
ご覧のとおり、親と必要なすべての変数を見つけることができます。create アクションの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 パラメータは間違って書かれています... 正しく書くのを手伝ってくれませんか? (このdiscussion_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) }