rspec

Rails not rolling back transaction after failed save()

心不动则不痛 提交于 2019-12-19 09:14:17
问题 I have this domain model: A user has group of items, and the state of the items can fail a validation. Validation works fine, and I even see exceptions get called when I use save! . In my controller, I have this: @user.items() << item if @user.save render :json => {}, :status => :ok else render :json => {:status => :error, :errors => item.errors}, :status => :bad_request end The first POST succeeds, and the second POST fails, but when I hit the index, I still see two objects, as if the second

Stubbing Warden on Controller Tests

删除回忆录丶 提交于 2019-12-19 08:33:07
问题 I'm having an issue with testing my controllers and using Warden. All examples point at stubbing request.env['warden'] . This causes issues in my controllers when I call env['warden'] , which then returns nil . For a crude example, using this: request.env['warden'] = double(Warden, :authenticate => nil, :authenticate! => nil, :authenticated? => false) And a simple before filter like this: before_filter do redirect_to new_user_session_url unless env['warden'].authenticated? end I get a nil . I

Stubbing Warden on Controller Tests

自闭症网瘾萝莉.ら 提交于 2019-12-19 08:32:14
问题 I'm having an issue with testing my controllers and using Warden. All examples point at stubbing request.env['warden'] . This causes issues in my controllers when I call env['warden'] , which then returns nil . For a crude example, using this: request.env['warden'] = double(Warden, :authenticate => nil, :authenticate! => nil, :authenticated? => false) And a simple before filter like this: before_filter do redirect_to new_user_session_url unless env['warden'].authenticated? end I get a nil . I

Checking existence of images and favicons with RSpec and Capybara

ぐ巨炮叔叔 提交于 2019-12-19 06:26:19
问题 Is there a good way to check on existence of images and favicons using rspec and capybara? I can check on the DOM of favicons and images, but I want to be able to check that those images load as well. Is this possible with rspec and capybara? 回答1: describe "check images and favicon" do before { visit "url/to/check") it "should have the images" do page.should have_css('img', text: "image1.jpg") it "should have the favicon" do page.should have_xpath("/html/head/link[@href='favicon.ico']" end

How to Stub out Warden/Devise with Rspec in Capybara test

99封情书 提交于 2019-12-19 05:43:23
问题 I want to stub out a logged in user (with Devise/Warden) using rspec mocks in a Capybara test suite in my Rails app. This would save a ton of time, and would mean that my test suite can/will be run regularly. Previously I was able to do this using authlogic by stubbing out my session model with some code like this: def login(user) user_session = mock_model(UserSession, {:user => user}) UserSession.stub(:find).and_return(user_session) end Now that i'm using Devise, i no longer have access to a

How to test send_data in RSpec? Or…what should I be testing for in this case?

久未见 提交于 2019-12-19 05:16:09
问题 What's the best way to test the following code using RSpec? And what should I be testing for? The show action opens a file and streams it. Also, if the action relies on a file existing somewhere, can I test that? def show image_option = params[:image_option] respond_to do |format| format.js format.pdf {open_bmap_file("#{@bmap.bmap_pdf_file}", 'application/pdf', "#{@bmap.bmap_name}.pdf", "pdf", "pdf")} format.png {open_bmap_file("#{@bmap.bmap_png_file}", 'image/png', "#{@bmap.bmap_name}.png",

How to test send_data in RSpec? Or…what should I be testing for in this case?

独自空忆成欢 提交于 2019-12-19 05:16:07
问题 What's the best way to test the following code using RSpec? And what should I be testing for? The show action opens a file and streams it. Also, if the action relies on a file existing somewhere, can I test that? def show image_option = params[:image_option] respond_to do |format| format.js format.pdf {open_bmap_file("#{@bmap.bmap_pdf_file}", 'application/pdf', "#{@bmap.bmap_name}.pdf", "pdf", "pdf")} format.png {open_bmap_file("#{@bmap.bmap_png_file}", 'image/png', "#{@bmap.bmap_name}.png",

RuntimeError: can't modify frozen Array when running rspec in rails 5.1

泄露秘密 提交于 2019-12-19 05:11:10
问题 I recently upgraded to Rails 5.1 from v4.3 and am now getting this error when running tests: An error occurred while loading ./spec/controllers/admin/capacity_charges_controller_spec.rb. Failure/Error: require File.expand_path('../../config/environment', __FILE__) RuntimeError: can't modify frozen Array I get it for every test file. The line that triggers the error comes from rails_helper. I've checked rails 5.1 sample repos and there's nothing substantially different about our version. The

In RSpec, using let variable inside before :all block

半城伤御伤魂 提交于 2019-12-19 05:05:15
问题 I have the following code inside most of my tests: describe 'index' let(:company) { FactoryGirl.create(:company) } let(:user) { FactoryGirl.create(:user, company: company) } before do sign_in user visit products_path end ... end But I'm getting the following warning: WARNING: let declaration 'user' accessed in a 'before(:all)' My question is, what is the correct way of doing this? I can't find much information about the warning itself. Thanks! EDIT: My goal is to use the user variable so I

How to test Controller post :create of JSON api on rails using rspec?

会有一股神秘感。 提交于 2019-12-19 04:50:18
问题 I have been tearing my hair trying to make the test to pass. I have a JSON API that looks like this: { "data": [ { "id": "b99f8173-0492-457f-9de9-6c1d8d6832ed", "type": "manufacturer_organizations", "attributes": { "account_number": "random test 123" }, "relationships": { "organization": { "data": { "id": "fb20ddc9-a3ee-47c3-bdd2-f710541ff89c", "type": "organizations" } }, "manufacturer": { "data": { "id": "1", "type": "manufacturers" } } } },... I am trying to make a post :create test in