-1

私はレールの初心者で、スタブとモックを使用してコントローラーコードをテストしようとしています。コードでモックを使用してみましたが、正しく実行できませんでした。リファクタリングされた正しいコードを教えてください。あなたが書いたコードの説明を手伝ってください。

require 'rails_helper'

RSpec.describe ArticlesController, type: :controller do
  let(:article) { create :article}
  let(:art_params) { attributes_for(:article) }
  let(:dbl) {double(:articles)}
  describe 'GET index' do
    it 'assigns @articles' do
      get :index
      allow(dbl).to receive(:articles).and_return article
      expect(dbl.articles).to eql(article)
      #expect_any_instance_of(Article).to receive(:save).and_return(true)
    end

    it 'renders the index template' do
      get :index
      allow(dbl).to receive(:articles)
      expect(response).to render_template('index')
    end
  end

  describe 'GET :new' do
    it 'render new template' do
      get :new
      expect(response).to render_template(:new)
    end
  end

  describe 'POST/create' do
    it 'created a new article ' do
    expect { post :create, article: art_params }.to change(Article, :count).by(1)
    end
  end

  describe 'POST/create not' do
    it 'did not create a new user' do
     # expect(art_params).to receive(attributes_for :article).with(content:)
      art_params = { article: attributes_for(:article, content: nil) }
      post :create, art_params
      expect(response).to render_template(:new)
    end
  end

  describe 'GET/edit' do
    it 'displays the edit template' do
      get :edit, id: article.id
      expect(response).to render_template(:edit)
    end
  end

  describe 'POST/update' do
    it 'displays the update template' do
      post :update, id: article.id, article: attributes_for(:article)
      expect(response).to redirect_to(article_path(article.id))
    end
  end

  describe 'POST/DELETE' do
    it 'destroys the article template' do
      dbl = double()
      article = create :article
      expect { delete :destroy, id: article.id }.to change(Article, :count).by(-1)

    end
  end
en

d

4

2 に答える 2

2

モッキングとスタブは、実際に正しく行うためのかなり高度なテストの概念です。真の理解がなければ、テストされたコードが変更されるたびに壊れる壊れやすいテストを簡単に作成できます。または常に合格するテスト。

たとえば、次のコードは失敗することはありません。テスト double ( dbl) を作成し、その double でメソッド ( ) をスタブし、#articlesこのメソッド呼び出しがインスタンスを返すように指定しますarticle#articlesテスト ダブルでその動作を定義したばかりなので、パスする必要があるテスト ダブルでメソッドを呼び出すことを期待しています。

let(:article) { create :article }
let(:dbl) { double(:articles) } # the test double

it 'assigns @articles' do
  get :index
  allow(dbl).to receive(:articles).and_return article # stubbing
  expect(dbl.articles).to eql(article) # invoking the stubbed method
end

その例から行を削除してみるとget :index、コードがまだ通過していることがわかります。

同様に、スタブなしで例を書くこともできます。

let(:article) { create :article }

it 'assigns @articles' do
  get :index
  expect(assigns(:articles)).to contain_exactly(article)
end

Rails と RSpec でプログラミングを開始する場合は、可能な限り実際のオブジェクトを使用することをお勧めします。rspec-rails のドキュメントには、多くの良い例があります : http://www.relishapp.com/rspec/rspec-rails/v/3-4/docsそれにもかなり良い場所です。

于 2016-04-06T17:38:06.817 に答える
1
  # This is a stub
  fake_article = allow(article).to receive(:compute_price).and_return(200)

  # This too
  another_fake_article = double(compute_price: 200)

  # This is a mock
  expect(article).to receive(:where).with(status: :closed)

記事がパラメーター ハッシュを使用してwhereメソッドを呼び出さない場合、最後のものは失敗します。{ status: :closed }それが役立つことを願っています。

于 2016-04-05T06:21:50.750 に答える