From e4a6bdb2e401c2466b75817ca976b55311415448 Mon Sep 17 00:00:00 2001 From: viehlieb Date: Thu, 13 Oct 2022 10:30:34 +0200 Subject: [PATCH] fix articles controller spec --- spec/controllers/articles_controller_spec.rb | 40 ++++++++++++++++---- spec/spec_helper.rb | 5 ++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/spec/controllers/articles_controller_spec.rb b/spec/controllers/articles_controller_spec.rb index 2fdee31a..56529200 100644 --- a/spec/controllers/articles_controller_spec.rb +++ b/spec/controllers/articles_controller_spec.rb @@ -12,7 +12,7 @@ describe ArticlesController, type: :controller do let(:supplier) { create :supplier, articles: [articleA, articleB] } let(:order) { create :order } - let(:order_article) { create :order_article, order: order, article: articleC } + before { login user } @@ -68,7 +68,7 @@ describe ArticlesController, type: :controller do expect(response).to have_http_status(:success) end - it 'fails to create a new article an renders #new' do + it 'fails to create a new article and renders #new' do get :create, params: { foodcoop: FoodsoftConfig[:default_scope], supplier_id: supplier.id, article: { id: nil } }, xhr: true expect(response).to have_http_status(:success) expect(response).to render_template('articles/new') @@ -114,6 +114,7 @@ describe ArticlesController, type: :controller do end describe "#update_selected" do + let(:order_article) { create :order_article, order: order, article: articleC } before do order_article end @@ -157,11 +158,11 @@ describe ArticlesController, type: :controller do end describe "#parse_upload" do - let(:file) { fixture_file_upload(Rails.root.join('spec/fixtures/files/upload_test.csv')) } + # let(:file) { fixture_file_upload(Rails.root.join('spec/fixtures/files/upload_test.csv')) } - before do - file - end + # before do + # file + # end # TODO: Cannot use Rack attributes in controller?? # #":String @@ -182,6 +183,7 @@ describe ArticlesController, type: :controller do end describe "#destroy" do + let(:order_article) { create :order_article, order: order, article: articleC } before do order_article end @@ -202,6 +204,7 @@ describe ArticlesController, type: :controller do end describe "#update_synchronized" do + let(:order_article) { create :order_article, order: order, article: articleC } before do order_article articleA @@ -290,10 +293,33 @@ describe ArticlesController, type: :controller do let(:supplier_with_shared) { create :supplier, articles: [articleS], shared_supplier: shared_supplier } it 'renders view with articles' do - get :shared, params: { foodcoop: FoodsoftConfig[:default_scope], supplier_id: supplier_with_shared.id, q: { name_cont_all: "shared" } }, xhr: true + get :shared, params: { foodcoop: FoodsoftConfig[:default_scope], supplier_id: supplier_with_shared.id, name_cont_all_joined: "shared" }, xhr: true expect(assigns(:supplier).shared_supplier.shared_articles.any?).to be_truthy expect(assigns(:articles).any?).to be_truthy expect(response).to have_http_status(:success) end end + + describe "#import" do + let(:shared_supplier) { create :shared_supplier, shared_articles: [shared_article] } + let(:shared_article) { create :shared_article, name: "shared" } + + before do + shared_article + article_categoryA + end + + it 'fills form with article details' do + get :import, params: { foodcoop: FoodsoftConfig[:default_scope], article_category_id: article_categoryB.id, direct: "true", supplier_id: supplier.id, shared_article_id: shared_article.id }, xhr: true + expect(assigns(:article).nil?).to be_falsey + expect(response).to have_http_status(:success) + expect(response).to render_template(:create) + end + it 'does redirect to :new if param :direct not set' do + get :import, params: { foodcoop: FoodsoftConfig[:default_scope], article_category_id: article_categoryB.id, supplier_id: supplier.id, shared_article_id: shared_article.id }, xhr: true + expect(assigns(:article).nil?).to be_falsey + expect(response).to have_http_status(:success) + expect(response).to render_template(:new) + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6cd41ded..666ab06f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,6 +21,10 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } RSpec.configure do |config| # We use capybara with webkit, and need database_cleaner + # config.before(:suite) do + # DatabaseCleaner.clean_with(:truncation) + # end + config.before(:each) do DatabaseCleaner.strategy = (RSpec.current_example.metadata[:js] ? :truncation : :transaction) DatabaseCleaner.start @@ -53,7 +57,6 @@ RSpec.configure do |config| config.include SpecTestHelper, type: :controller config.include SessionHelper, type: :feature - # Automatically determine spec from directory structure, see: # https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/directory-structure config.infer_spec_type_from_file_location!