Created
December 31, 2015 00:39
-
-
Save jkarnowski/e52fee2836d81351f3df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe ClearanceBatchesController, type: :controller do | |
subject(:clearancing_service) { ClearancingService.new } | |
def expect_response_to_render_index | |
expect(response).to render_template :index | |
end | |
describe "GET #index" do | |
let(:clearance_batch) { FactoryGirl.create(:clearance_batch) } | |
let(:item) { FactoryGirl.create(:item) } | |
it 'populates an array of clearance batches' do | |
get :index | |
assigns(:clearance_batch).to eq([clearance_batch]) | |
end | |
it "renders the index page" do | |
get :index | |
expect(response).to have_http_status(:success) | |
end | |
end | |
describe "POST #create #process_item" do | |
context "with a valid item" do | |
it "saves a new item with valid input" do | |
item = FactoryGirl.attributes_for(:item) | |
expect{ | |
post :create, | |
item: item | |
}.to change(ClearanceBatch, :count).by(1) | |
expect_response_to_render_index | |
end | |
it "redirects to the index page" do | |
post :create, item: FactoryGirl.attributes_for(:item) | |
response.should redirect_to :index | |
end | |
end | |
context "with invalid item" do | |
it "does not save the item with invalid item id" do | |
expect{ | |
post :create, | |
item: FactoryGirl.attributes_for(:item, :item_id => nil) | |
}.to_not change(ClearanceBatch).by(1) | |
end | |
it "re-renders the home page" do | |
expect{ | |
post :create, | |
item: FactoryGirl.attributes_for(:item, :item_id => nil) | |
}.to render_template :index | |
end | |
end | |
end | |
describe "POST #create #process_file" do | |
context "when a valid CSV file is uploaded" do | |
let(:items) { 5.times.map{ FactoryGirl.create(:item) } } | |
let(:file_name) { generate_csv_file(items) } | |
let(:uploaded_file) { Rack::Test::UploadedFile.new(file_name) } | |
it "creates a new clearance batch with valid items from file" do | |
expect{ | |
post :create, | |
items: uploaded_file | |
}.to change(ClearanceBatch, :count).by(1) | |
end | |
end | |
context "when an invalid CSV file is uploaded" do | |
it "does not save a clearance batch " do | |
end | |
end | |
end | |
describe "#check_status_of_items" do | |
context "when a new clearance batch with new items persists" do | |
let(:items) { 5.times.map { FactoryGirl.create(:item) } } | |
let(:file_name) { generate_csv_file(items) } | |
let(:uploaded_file) { Rack::Test::UploadedFile.new(file_name) } | |
it "sets the flash notice for a new clearance batch" do | |
post :create | |
expect(flash[:notice]).to eq("#{@clearance_status.clearance_batch.items.count} items clearanced") | |
end | |
it "sets a flash alert for no new clearance batch" do | |
expect(flash[:alert]).to eq("No new clearance batch was added") | |
end | |
end | |
context "when a new clearance batch did not contain new items to be clearanced" do | |
let(:items) { 5.times.map { FactoryGirl.create(:item) } } | |
let(:file_name) { generate_csv_file(items) } | |
let(:uploaded_file) { Rack::Test::UploadedFile.new(file_name) } | |
it "sets a flash alert with item ids that were not clearanced" do | |
expect(flash[:alert]).to eq("#{@clearancing_status.errors.count} item ids raised errors and were not clearanced") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment