2017-11-30 17 views
0

私はRSpecを初めて使っていますが、ここではこのコードに基づいてテストを作成しようとしていますが、このエラーが発生しています。助言がありますか?
CODE:条件コードのRspec if-else?

serialization_scope nil 
    before_action :set_list, only: [:show, :destroy, :update] 
    before_action :verify_user, only: :show 


    def create 
    @list = current_user.lists.build(list_params) 

    if @list.save 

     render json: {message: ['Success']}, status: 200 
    else 
     render json: {errors:[@list.errors.full_messages]}, status: 400 
    end 
    end 
ここ

は、私が始めたRSpecのファイルです:

require "rails_helper" 

RSpec.describe V1::ListsController, :type => :controller do 

    describe "POST create" do 
    it "returns HTTP status" do 
     expect(post :create).to change(@list, :count).by(+1) 
     expect(response).to have_http_status :success #200 
    end 
    end 

    describe 'GET status if its not created' do 
    it "return HTTP status - reports BAD REQUEST (HTTP status 400)" do 
     expect(response.status).to eq 400 
    end 
    end 
end 

そして、私が得たエラーは次のとおりです。

スペックは次のようになり
Failures: 

    1) V1::ListsController GET status if its created returns HTTP status 
    Failure/Error: expect(post :create).to change(@list, :count).by(+1) 
     expected #count to have changed by 1, but was not given a block 
    # ./spec/controllers/lists_controller_spec.rb:8:in `block (3 levels) in <top (required)>' 

    2) GET status if its not created return HTTP status - reports BAD REQUEST (HTTP status 400) 
    Failure/Error: expect(response.status).to eq 400 

     expected: 400 
      got: 200 

     (compared using ==) 

答えて

1

describe "POST create" do 
    context 'valid request' do 
    it 'should increase @list item' do 
     expect { post :create }.to change(List, :count).by(1) 
    end 

    it "returns HTTP status" do 
     post :create 
     expect(response).to have_http_status :success #200 
    end 
    end 

    context 'invalid request' do 
    it "return HTTP status - reports BAD REQUEST (HTTP status 400)" do 
     get :create 
     expect(response.status).to eq 400 
    end 
    end 
end 

乾杯!

+0

お返事ありがとうございました。しかし、私はここで同じエラーが発生しています 1)V1 :: ListsController POSTは有効なリクエストを増やします@list item エラー/エラー:失敗/エラー:expect(post:create).to(list、:count)を変更します。 )) 予想数#1が変更されたが、ブロックが与えられていない – Yosh

+1

変更 'expect {投稿:作成}' 'get:create expect(response.status)' – Manishh

+0

正確には、 'change'にマッチさせる。 – rony36

0

このコードを試してください。

require 'rails_helper' 

RSpec.describe V1::ListsController, type: :request do 
    describe 'valid request' do 
    it 'returns HTTP status' do 
     post '/list', params: { list: { list_name: 'xyz' } } 
     expect(response.status).to eq 201 
    end 
    end 
    describe 'invalid request' do 
     it "should return unauthorized" do 
     post '/list' 
     assert_response :unauthorized 
     end 
    end 
end 

paramsでは、list_paramsを渡す必要があります。

0

意図的にいくつかのバリデーションを失敗させることによって、作成されていないオブジェクトをテストすることができます。 RSpecから必須の属性としてnilを渡すことができます。

サンプルリクエスト:post :create, { title: nil }

あなたのRSpecコードによれば、Listモデルには検証がないようです。したがって、この特定のテストのためにsaveをスタブしてfalseに戻すようにしてください。

describe 'GET status if its not created' do 
    # Assuming your model name is `List` 
    before { allow_any_instance_of(List).to receive(:save) { false } } 
    it "return HTTP status - reports BAD REQUEST (HTTP status 400)" do 
    post :create 
    expect(response.status).to eq 400 
    end 
end 

listのモデルを投稿してください。より適切なテストで回答を更新することができます。

0

Ishika、私はあなたを助けることができるなら、私は見てみましょう:)

RSpecの公式ドキュメントでは、要求仕様の代わりに、コントローラの仕様を使用することを推奨しています。これは、Rails 5がコントローラのテストに使用されているいくつかのメソッドを非推奨にしているため、お勧めします。この詳細については、here at RSpec blog

ps:これまでコントローラテストを使用できますが、RSpecの将来のメジャーバージョンでは非推奨にすることができます。

コードの後ろにメモがありますので、それらもお読みください。

# spec/requests/v1/lists_controller_create_spec.rb 

require "rails_helper" 

RSpec.describe V1::ListsController do 
    describe 'success' do 
    it 'returns ok and creates a list', :aggregate_failures do # :aggregate_failures is available only for RSpec 3.3+ 
     expect do 
     post '/list', title: 'foo' # This will also test your route, avoiding routing specs to be necessary 
     end.to change { List.count }.from(0).to(1) 

     expect(response).to have_http_status(:ok) 
    end 
    end 

    describe 'bad request' do 
    before do 
     # This is needed because your controller is not validating the object, but look at my 
     # comment below (out of the code), to think about this behavior, please. 
     allow_any_instance_of(List).to receive(:save).and_return(false) 
    end 

    it 'returns a bad request and does not create a list' do 
     expect do 
     post '/list', title: 'foo' # This will also test your route, avoiding routing specs to be necessary 
     end.not_to change { List.count } 

     expect(response).to have_http_status(:bad_request) 
    end 
    end 
end 

注:

私はこのような要求仕様記述し、彼らは単純で、理由はので、私は例によって1人の以上の期待を使うことを提案

  1. を、それがこの仕様でokです私は:aggregate_failuresオプションを使用しています。このオプションでは、最初の期待値が失敗した場合、次の期待値も最初のものに依存しないので、次の期待値も実行されます。 Reference
  2. オブジェクトが保存されていないが、検証していない場合は、不正な要求が返されます。モデルにそのオブジェクトを検証する検証がある場合は、使用したモックを使用する代わりに、保存に失敗するように仕様を調整し、応答内にエラーメッセージを表示することを検討してください。
  3. postexpectブロック、あなたは違うことができます:変数を変更するかどうかをテストする投稿と投稿の前に、変数にリストの数を格納します。おそらくもっと明確になり、まったく同じことをするでしょうバックグラウンドで