Controller spec api improvements
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
# encoding: UTF-8
|
||||
require 'spec_helper'
|
||||
|
||||
describe Suppliers::ListsController, type: :controller do
|
||||
before :all do
|
||||
c = List
|
||||
class List
|
||||
attr_accessor :foo
|
||||
validates :foo, format: {with: /\Aqqq\z/, if: ->(l){ l.foo.present? }}
|
||||
end
|
||||
end
|
||||
before :each do
|
||||
@supplier = Supplier.find_by_email('supplier@mozo.bar') || create(:supplier, :confirmed)
|
||||
ActionController::Parameters.permit_all_parameters = false
|
||||
@@ -82,11 +88,6 @@ describe Suppliers::ListsController, type: :controller do
|
||||
}.to change(List, :count).by(1)
|
||||
end
|
||||
|
||||
it "redirects to the new list" do
|
||||
post :create, list: valid_attributes
|
||||
response.should redirect_to [:suppliers, List.last]
|
||||
end
|
||||
|
||||
it "should not be possible to create a list for another supplier" do
|
||||
supplier2 = create :supplier
|
||||
expect{ post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id) }.to raise_error
|
||||
@@ -97,10 +98,15 @@ describe Suppliers::ListsController, type: :controller do
|
||||
context "with invalid attributes" do
|
||||
it "does not save the new list" do
|
||||
expect{
|
||||
post :create, list: invalid_params
|
||||
}.to raise_error
|
||||
post :create, format: :json, list: invalid_params
|
||||
}.not_to change{ List.count }
|
||||
List.count.should be_zero
|
||||
end
|
||||
|
||||
it 'returns an error object' do
|
||||
post :create, format: :json, list: invalid_params
|
||||
expect( JSON.parse(response.body)['errors'] ).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -122,11 +128,6 @@ describe Suppliers::ListsController, type: :controller do
|
||||
@list.price.should eq(7.22)
|
||||
end
|
||||
|
||||
it "redirects to the updated list" do
|
||||
put :update, id: @list, list: valid_attributes
|
||||
response.should redirect_to [:suppliers, @list]
|
||||
end
|
||||
|
||||
it "should not be possible to update a list to another supplier" do
|
||||
supplier2 = create :supplier
|
||||
expect{ put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id) }.to raise_error
|
||||
@@ -143,8 +144,9 @@ describe Suppliers::ListsController, type: :controller do
|
||||
end
|
||||
|
||||
context "invalid attributes" do
|
||||
it "raises on invalid params" do
|
||||
expect{ put :update, id: @list, list: invalid_params }.to raise_error
|
||||
it "returns an error response" do
|
||||
put :update, id: @list, format: :json, list: invalid_params
|
||||
expect( JSON.parse( response.body )['errors']).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -160,11 +162,6 @@ describe Suppliers::ListsController, type: :controller do
|
||||
}.to change(List, :count).by(-1)
|
||||
end
|
||||
|
||||
it "redirects to lists#index" do
|
||||
delete :destroy, id: @list
|
||||
response.should redirect_to [:suppliers, :lists]
|
||||
end
|
||||
|
||||
it "should not be possible to delete a list of another supplier" do
|
||||
list = create :list
|
||||
expect{
|
||||
|
||||
Reference in New Issue
Block a user