mercredi 29 novembre 2017

Rspec for conditional code if-else?

I am new to RSpec but here I am trying to create tests based on this code and I am keep on getting this error. Any suggestions?
CODE:

  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

Here is the RSpec file that I started :

require "rails_helper"

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

  describe "POST create" do
    it "returns HTTP status" do
      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

And the error that I got is :

.F

Failures:

  1) GET #watchlist 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 ==)

Aucun commentaire:

Enregistrer un commentaire