私は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)V1 :: ListsController POSTは有効なリクエストを増やします@list item エラー/エラー:失敗/エラー:expect(post:create).to(list、:count)を変更します。 )) 予想数#1が変更されたが、ブロックが与えられていない – Yosh
変更 'expect {投稿:作成}' 'get:create expect(response.status)' – Manishh
正確には、 'change'にマッチさせる。 – rony36