How can I describe of finding an associated model in the RSpec-rails controller examples?
I try the following:
Article
class Article < ActiveRecord::Base
has_many :comments
end
Comment
class Comment < ActiveRecord::Base
end
CommentsControllerSpec
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
before { Article.stub(:find) { article } }
before { article.stub(:comments) { [comment] } }
it 'finds a comment' do
article.comments.should_receive(:find) { comment }
put :update, article_id: article.id, id: comment.id
end
end
end
CommentsController
class CommentsController < ApplicationController
def update
Article.find.comments.find
end
end
But unfortunatelly it doesn’t work. Any ideas?
Thanks.
I tackled the problem by myself.
Here’s the approximate list of actions to describe of finding an associated model in the Rails’ controllers.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.