Testing with rspec a create method in Controller which uses Devise and respond_with json format

Testing with rspec a create method in Controller which uses Devise and respond_with json format

This is the controller and the method create wich repond_with json format:

The file located in app/controller/invoices_controller.rb


# coding: utf-8
class InvoicesController < ApplicationController
  before_filter :authenticate_user!
  respond_to :json

  def create
    respond_with current_user.invoices.create(params[:invoice])
  end
end

And the rspec test located in spec/controllers/invoices_controller_spec.rb

# coding: utf-8
require 'spec_helper'
describe InvoicesController do
  let(:current_user)do
    FactoryGirl.create(:user)
  end

  before do
    controller.should_receive(:authenticate_user!)
    controller.stub(:current_user).and_return current_user
  end

  describe '#create' do
    context 'when an invoice is created' do
      let(:invoice_params) do
      {
        tipo_comprobante: 'Factura',
        total: 3400.00,
        subtotal: 500.50,
        folio: 333,
        address_id: 1,
        fecha_emision: '13/06/2013',
        partner_id: 1
      }
      end

      it 'successfully' do
        post :create, { invoice: invoice_params, format: :json }
        expect(response).to be_success
        expect(JSON(response.body)['tipo_comprobante']).to eq('Factura')
      end
    end
  end
end
No Comments

Post A Comment