add admin suppliers controller specs

This commit is contained in:
2012-12-05 01:29:56 +01:00
parent fe7c061d37
commit e1e1ad3af1
4 changed files with 142 additions and 46 deletions
+2
View File
@@ -28,6 +28,8 @@ class Supplier
view :by_email, key: :email
validates :name, presence: true
def location=(val)
lat, lng = val.strip.split(/[ ,]+/).map(&:to_f)
self.lat = lat
+4 -46
View File
@@ -1,50 +1,8 @@
= form_for @supplier, html: {class: 'form-horizontal' } do |f|
= simple_form_for [:admin, @supplier], html: {class: 'form-horizontal' } do |f|
= render 'error_messages', target: @supplier
.control-group class=(@supplier.errors[:email].any? ? 'error' : nil)
= f.label :email, class: 'control-label'
.controls
= f.text_field :email, class: 'text_field'
.control-group class=(@supplier.errors[:encrypted_password].any? ? 'error' : nil)
= f.label :encrypted_password, class: 'control-label'
.controls
= f.text_field :encrypted_password, class: 'text_field'
.control-group class=(@supplier.errors[:remember_created_at].any? ? 'error' : nil)
= f.label :remember_created_at, class: 'control-label'
.controls
= f.text_field :remember_created_at, class: 'text_field'
.control-group class=(@supplier.errors[:reset_password_token].any? ? 'error' : nil)
= f.label :reset_password_token, class: 'control-label'
.controls
= f.text_field :reset_password_token, class: 'text_field'
.control-group class=(@supplier.errors[:reset_password_sent_at].any? ? 'error' : nil)
= f.label :reset_password_sent_at, class: 'control-label'
.controls
= f.text_field :reset_password_sent_at, class: 'text_field'
.control-group class=(@supplier.errors[:sign_in_count].any? ? 'error' : nil)
= f.label :sign_in_count, class: 'control-label'
.controls
= f.text_field :sign_in_count, class: 'text_field'
.control-group class=(@supplier.errors[:current_sign_in_at].any? ? 'error' : nil)
= f.label :current_sign_in_at, class: 'control-label'
.controls
= f.text_field :current_sign_in_at, class: 'text_field'
.control-group class=(@supplier.errors[:last_sign_in_at].any? ? 'error' : nil)
= f.label :last_sign_in_at, class: 'control-label'
.controls
= f.text_field :last_sign_in_at, class: 'text_field'
.control-group class=(@supplier.errors[:current_sign_in_ip].any? ? 'error' : nil)
= f.label :current_sign_in_ip, class: 'control-label'
.controls
= f.text_field :current_sign_in_ip, class: 'text_field'
.control-group class=(@supplier.errors[:last_sign_in_ip].any? ? 'error' : nil)
= f.label :last_sign_in_ip, class: 'control-label'
.controls
= f.text_field :last_sign_in_ip, class: 'text_field'
.control-group class=(@supplier.errors[:name].any? ? 'error' : nil)
= f.label :name, class: 'control-label'
.controls
= f.text_field :name, class: 'text_field'
= f.input :name
= f.input :email
.form-actions
= f.submit nil, class: 'btn btn-primary'
'
= link_to t("helpers.links.cancel"), suppliers_path, class: 'btn'
= link_to t("helpers.links.cancel"), admin_suppliers_path, class: 'btn'
@@ -0,0 +1,135 @@
# encoding: UTF-8
require 'spec_helper'
describe Admin::SuppliersController do
before :each do
@administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret')
sign_in @administrator
end
describe "GET #index" do
it "populates an array of suppliers" do
supplier = create :supplier
get :index
assigns(:suppliers).should eq([supplier])
end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end
it "renders the :index view" do
get :index
response.should render_template :index
end
end
describe "GET #show" do
it "assigns the requested supplier to @supplier" do
supplier = create :supplier
get :show, id: supplier
assigns(:supplier).should eq(supplier)
end
it "renders the #show view" do
supplier = create :supplier
get :show, id: supplier
response.should render_template :show
end
end
describe "GET #new" do
it "assigns a new supplier to @supplier" do
get :new
assigns(:supplier).should be_a Supplier
end
it "renders the #show view" do
get :new
response.should render_template :new
end
end
describe "POST #create" do
context "with valid attributes" do
it "creates a new supplier" do
expect{
post :create, supplier: attributes_for(:supplier)
}.to change(Supplier, :count).by(1)
end
it "redirects to the new supplier" do
post :create, supplier: attributes_for(:supplier)
response.should redirect_to [:admin, Supplier.last]
end
end
context "with invalid attributes" do
it "does not save the new supplier" do
expect{
post :create, supplier: {}
}.to_not change(Supplier, :count)
end
it "re-renders the new method" do
post :create, supplier: {}
response.should render_template :new
end
end
end
describe 'PUT update' do
before :each do
@supplier = create :supplier
end
context "valid attributes" do
it "located the requested supplier" do
put :update, id: @supplier, supplier: attributes_for(:supplier)
@supplier.reload
assigns(:supplier).should eq(@supplier)
end
it "changes @supplier's attributes" do
put :update, id: @supplier, supplier: attributes_for(:supplier, name: "ChangedByTest")
@supplier.reload
@supplier.name.should eq("ChangedByTest")
end
it "redirects to the updated supplier" do
put :update, id: @supplier, supplier: attributes_for(:supplier)
response.should redirect_to [:admin, @supplier]
end
end
context "invalid attributes" do
it "locates the requested supplier" do
put :update, id: @supplier, supplier: {name: ''}
assigns(:supplier).should eq(@supplier)
end
it "re-renders the edit method" do
put :update, id: @supplier, supplier: {name: ''}
response.should render_template :edit
end
end
end
describe 'DELETE destroy' do
before :each do
@supplier = create :supplier
end
it "deletes the supplier" do
expect{
delete :destroy, id: @supplier
}.to change(Supplier, :count).by(-1)
end
it "redirects to suppliers#index" do
delete :destroy, id: @supplier
response.should redirect_to [:admin, :suppliers]
end
end
end
+1
View File
@@ -1,5 +1,6 @@
FactoryGirl.define do
factory :supplier do
sequence(:name){|i| "Supplier #{i}"}
password 'secret'
end
end