如何描述在RSpec-rails控制器示例中查找关联模型?
我尝试以下方法:
文章
class Article < ActiveRecord::Base
has_many :comments
end
注释
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
但不幸的是,它不起作用。有什么想法吗?
感谢。
答案 0 :(得分:3)
我自己解决了这个问题。
以下是在Rails控制器中查找关联模型的动作的近似列表。
<强> 1 强>
describe CommentsController do
describe 'update' do
end
end
<强> 2 强>
describe CommentsController do
describe 'update' do
it 'finds an article' # <=
end
end
第3 强>
describe CommentsController do
describe 'update' do
it 'finds an article' do
Article.should_receive(:find) # <=
put :update # <=
end
end
end
<强> 4 强>
No route matches {:controller=>"comments", :action=>"update"}
<强> 5 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article } # <=
it 'finds a article' do
Article.should_receive(:find)
put :update, article_id: article.id # <=
end
end
end
<强> 6 强>
No route matches \
{:article_id=>"1025", :controller=>"comments", :action=>"update"}
<强> 7 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment } # <=
it 'finds an article' do
Article.should_receive(:find)
put :update, article_id: article.id, id: comment.id # <=
end
end
end
<强> 8 强>
(<Article (class)>).find(any args)
expected: 1 time
received: 0 times
<强> 9 强>
class CommentsController < ApplicationController
def update
Article.find
end
end
<强> 10 强>
Success!
<强> 11 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) # <=
put :update, article_id: article.id, id: comment.id
end
end
end
<强> 12 强>
<Article (class)> received :find with unexpected arguments
expected: ("1025")
got: (no args)
<强> 13 强>
class CommentsController < ApplicationController
def update
Article.find(params[:article_id]) # <=
end
end
<强> 14 强>
Success!
<强> 15 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) { # <=
mock(ActiveRecord::Associations::HasManyAssociation) # <=
} # <=
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s)
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do # <=
article.stub(:comments) { association } # <=
association.should_receive(:find) # <=
put :update, article_id: article.id, id: comment.id # <=
end # <=
end
end
<强> 16 强>
Couldn't find Article with id=1027
<强> 17 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
mock(ActiveRecord::Associations::HasManyAssociation)
}
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s)
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article } # <=
article.stub(:comments) { association }
association.should_receive(:find)
put :update, article_id: article.id, id: comment.id
end
end
end
<强> 18 强>
(Mock ActiveRecord::Associations::HasManyAssociation).find(any args)
expected: 1 time
received: 0 times
<强> 19 强>
class CommentsController < ApplicationController
def update
Article.find(params[:article_id]).comments.find # <=
end
end
<强> 20 强>
update
finds an article (fail!)
finds a comment (success!)
undefined method `comments' for nil:NilClass
<强> 21 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
mock(ActiveRecord::Associations::HasManyAssociation)
}
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) { article } # <=
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article }
article.stub(:comments) { association }
association.should_receive(:find)
put :update, article_id: article.id, id: comment.id
end
end
end
<强> 22 强>
update
finds an article (fail!)
finds a comment (success!)
Couldn't find Comment without an ID
<强> 23 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
mock(ActiveRecord::Associations::HasManyAssociation)
}
before { article.stub(:comments) { association } } # <=
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) { article }
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article }
# -> article.stub(:comments) { association } <-
association.should_receive(:find)
put :update, article_id: article.id, id: comment.id
end
end
end
<强> 24 强>
update
finds an article (fail!)
finds a comment (success!)
Mock ActiveRecord::Associations::HasManyAssociation \
received unexpected message :find with (no args)
<强> 25 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
mock(ActiveRecord::Associations::HasManyAssociation).as_null_object # <=
}
before { article.stub(:comments) { association } }
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) { article }
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article }
association.should_receive(:find)
put :update, article_id: article.id, id: comment.id
end
end
end
<强> 26 强>
Success!
<强> 27 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
mock(ActiveRecord::Associations::HasManyAssociation).as_null_object
}
before { article.stub(:comments) { association } }
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) { article }
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article }
association.should_receive(:find).with(comment.id.to_s) # <=
put :update, article_id: article.id, id: comment.id
end
end
end
<强> 28 强>
Mock ActiveRecord::Associations::HasManyAssociation \
received :find with unexpected arguments
expected: ("1028")
got: (no args)
<强> 29 强>
class CommentsController < ApplicationController
def update
Article.find(params[:article_id]).comments.find(params[:id]) # <=
end
end
<强> 30 强>
Success!
<强> 31 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
double(ActiveRecord::Associations::HasManyAssociation).as_null_object # <=
}
before { article.stub(:comments) { association } }
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) { article }
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article }
association.should_receive(:find).with(comment.id.to_s)
put :update, article_id: article.id, id: comment.id
end
it 'assigns @comment' do # <=
Article.stub(:find) { article } # <=
association.stub(:find) { comment } # <=
put :update, article_id: article.id, id: comment.id # <=
assigns[:comment].should eq comment # <=
end # <=
end
end
<强> 32 强>
expected: #<Comment >
got: nil
<强> 33 强>
class CommentsController < ApplicationController
def update
@comment = Article.find(params[:article_id]).comments.find(params[:id]) # <=
end
end
<强> 34 强>
Success!
<强> 35 强>
describe CommentsController do
describe 'update' do
let(:article) { stub_model Article }
let(:comment) { stub_model Comment }
let(:association) {
double(ActiveRecord::Associations::HasManyAssociation).as_null_object
}
before { article.stub(:comments) { association } }
it 'finds an article' do
Article.should_receive(:find).with(article.id.to_s) { article }
put :update, article_id: article.id, id: comment.id
end
it 'finds a comment' do
Article.stub(:find) { article }
association.should_receive(:find).with(comment.id.to_s)
put :update, article_id: article.id, id: comment.id
end
it 'assigns @article' do # <=
Article.stub(:find) { article } # <=
put :update, article_id: article.id, id: comment.id # <=
assigns[:article].should eq article # <=
end # <=
it 'assigns @comment' do
Article.stub(:find) { article }
association.stub(:find) { comment }
put :update, article_id: article.id, id: comment.id
assigns[:comment].should eq comment
end
end
end
<强> 36 强>
expected: #<Article >
got: nil
<强> 37 强>
class CommentsController < ApplicationController
def update
@article = Article.find(params[:article_id]) # <=
@comment = @article.comments.find(params[:id]) # <=
end
end
<强> 38 强>
Success!
<强> 39 强>
class CommentsController < ApplicationController
before_filter :find_article, only: :update # <=
before_filter :find_comment, only: :update # <=
def update
# -> @article = Article.find(params[:article_id]) <-
# -> @comment = @article.comments.find(params[:id]) <-
end
private # <=
def find_article # <=
@article = Article.find(params[:article_id]) # <=
end # <=
def find_comment # <=
@comment = @article.comments.find(params[:id]) # <=
end # <=
end
<强> 40 强>
Success!
答案 1 :(得分:1)
我会说,替换:
before { article.stub(:comments) { [comment] } }
使用:
before { article.stub_chain(:comments, :find) { [comment] } }