RSpec-rails |查找关联模型的说明

时间:2011-12-17 14:18:23

标签: ruby-on-rails rspec rspec-rails

如何描述在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

但不幸的是,它不起作用。有什么想法吗?

感谢。

2 个答案:

答案 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] } }