Prueba realizada con rspec capybara usando mocks y stub

Prueba realizada con rspec capybara usando mocks y stub

Codigo del Controller

class SessionsController < ApplicationController
  respond_to :json, only: :create

  def create
    sign_in_service = service :sign_in, params
    @result = sign_in_service.call

    if @result.valid?
      Services::SessionHandlerService.new(self).create @result.object
      render status: :ok, json: []
    else
      render status: :unprocessable_entity, json: @result.errors
    end
   end
end
end

Codigo de la Prueba

require 'spec_helper'

describe SessionsController do
 describe "#create" do
 let(:params) do
   { email: "test@test.com", password: "123test123" }
 end

 let(:service) do
   mock 'SignInService', call: result
 end

 before do
   subject.should_receive(:service).and_return service
 end

context "when success" do
  let(:result) do
  mock 'Result', valid?: true, object: mock
end

 specify do
   post :create, params, format: :json
   expect(response).to be_success
 end
 end
 context "when fails" do
   let(:result) do
   mock 'Result', valid?: false, object: mock, errors: errors
 end

let(:errors) do
   { "password" => [ "is too short (minimum is 8 characters)",
   "Must include letters and numbers only",
    "can't be blank" ],
    "email" => [ "Email can't be blank",
   "Email is invalid" ]
   }
 end
  specify do
    post :create, params, format: :json
    expect(response.status).to eq(422)
    expect(response.body).to eq(errors.to_json)
   end
  end
 end
end
Tags:
No Comments

Post A Comment