Skip to content

Instantly share code, notes, and snippets.

@thiagoramos23
Created October 23, 2014 10:05
Show Gist options
  • Save thiagoramos23/55e26ea85d7f9fec893b to your computer and use it in GitHub Desktop.
Save thiagoramos23/55e26ea85d7f9fec893b to your computer and use it in GitHub Desktop.
Create Residence Spec
require 'rails_helper'
RSpec.describe Admin::ResidencesController do
let(:user) { double('user') }
let(:valid_params) { attributes_for(:residence) }
context "Valid User" do
let(:setup) {}
before {
sign_in user
setup
}
describe "#create" do
context "valid params" do
let(:setup) { allow(Residence).to receive(:create) { residence_101 } }
let(:residence_101) { build_stubbed(:residence) }
let(:permitted_attributes) {
[:housing_situation, :localization, :type_residence,
:conditions_of_land_use, :residence_type_access,
:predominant_material_construction_of_external_wall,
:type_water_supply, :type_water_treatment,
:ways_of_disposing_bathroom, :garbage_disposal, :animal_species,
:type_of_patio, :name_of_patio, :number, :uf, :municipality,
:cep, :housing_situation, :localization
]
}
before {
post :create, residence: valid_params
}
it { is_expected.to permit(*permitted_attributes).for(:create) }
it_behaves_like "redirectable action", "index action for @residences" do
let(:path) { admin_residences_path }
end
it "calls create method and set to a persisted @residence" do
expect(assigns(:residence)).to be_persisted
end
end
context 'invalid params' do
let(:invalid_residence) { build(:residence, cep: "") }
let(:setup) { allow(Residence).to receive(:create) {
invalid_residence.valid?
invalid_residence
}
}
let(:invalid_params) { attributes_for(:residence, cep: "") }
before {
post :create, residence: invalid_params
}
it_behaves_like "renderable action", :new
it "expects to not persist residence" do
expect(assigns(:residence)).to_not be_persisted
end
end
end
end
include_context 'redirect to sign in' do
let(:before_action) { post :create, residence: valid_params}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment