rspec

RSpec for should_receive and should_not_receive both passed for Exception

三世轮回 提交于 2020-01-02 01:48:07
问题 I had a really weird rspec case scenario. I tried to test if my function handles exception correctly. And the following is my code: in User.rb: def welcome_user begin send_welcome_mail(self) rescue Exception => exception ErrorMessage.add(exception, user_id: self.id) end end end in user_spec.rb it "adds to error message if an exception is thrown" do mock_user = User.new mock_user.stub(:send_welcome_mail).and_raise(Exception) ErrorMessage.should_receive(:add) mock_user.welcome_user end The test

Receiving error in RSpec for PUT, but not POST

☆樱花仙子☆ 提交于 2020-01-01 19:59:10
问题 In my specs, when I run the POST request below, everything works fine. before do request_payload = { player: { first_name: "Joe", last_name: "Carradine", team_id: "1" } } post :create, request_payload end But when I run a spec for PUT: before do request_payload = { player: { first_name: "Buck", last_name: "Carradine", team_id: "1" } } put :update, id: 3, request_payload end I get an error like this: [filename]_spec.rb:139: syntax error, unexpected '\n', expecting tASSOC (SyntaxError)

Capybara NoMethodError: undefined method `visit' for

放肆的年华 提交于 2020-01-01 18:28:26
问题 Thank for response. I getting error in application Git application with the error Failures: 1) Page pages page creation with invalid information should not create a page Failure/Error: before(:each) { visit new_admin_page_path } NoMethodError: undefined method `visit' for #<Page:0x00000005094bb0> # ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>' 2) Page pages page creation with invalid information error messages Failure/Error: before(:each) { visit new_admin

Getting RSpec to report intermediate failures

梦想与她 提交于 2020-01-01 18:17:06
问题 Is there a way to get RSpec to report failures as they occur, instead of the long list of green/red dots? I'm running RSpec 2.5.0. 回答1: RSpec supports multiple output formats, specified with the --format option. The default is "progress", the string of dots you're seeing. If you change to a more verbose format, you'll see the name of the test in red/green as you run, but you still won't see what the failure was until the summary that's printed at the end. The other format options, from

How to stub a class method using rspec/rspec-mocks

旧街凉风 提交于 2020-01-01 11:49:29
问题 I am using rspec-mock for test-driven-development. I am starting implementing a single class and mocking/stubbing the other classes using rspec-mock. Mocking objects of classes yet to be implemented works well. However when I try to mock a class method of a class that does not exist yet, I haven't been successful. My class "Hashes" should have a class method "calculate_hashes" receiving a filename and returning a hash. I tried allow(Hashes).to receive(:calculate_hash) do |file| # looks up

How to remove “DEPRECATION: stub! is deprecated. Use stub instead.” message?

核能气质少年 提交于 2020-01-01 09:53:46
问题 The question is a bit odd formulated but what I want is an alternative for stub! in Rspec which doesn't produce a deprecation warning. The scenario: I use stub! to stub certain helper methods in my helper spec. For instance stub!(:t_with_partner_lookup).and_return("test") Rspec than suggests to use stub without the exclamation mark. So I write (as suggested): stub(:t_with_partner_lookup).and_return("test") However this produces an error: Stub :t_with_partner_lookup received unexpected message

Rails/RSpec toggle cache for a single test

匆匆过客 提交于 2020-01-01 09:33:59
问题 So in my app I can disable the cache for all tests, which would be ideal, but apparently there are a number of legacy tests that rely on the cache being functional. Is there a way to enable the Rails cache for a single RSpec test? Something like: before(:each) do @cache_setting = Rails.cache.null_cache Rails.cache.null_cache = true end after(:each) do Rails.cache.null_cache = @cache_setting end it 'does not hit the cache' do ... end 回答1: in spec_helper.rb RSpec.configure do |config| config

How to upload a file in a Rails Rspec request spec

China☆狼群 提交于 2020-01-01 08:29:09
问题 I want to test uploading a file in a Rails Rspec request test. I'm using Paperclip for asset storage. I've tried: path = 'path/to/fixture_file' params = { file: Rack::Test::UploadedFile.new(path, 'application/pdf', true) } post v1_product_documents_path, params: params In the controller, I get a string "#Rack::Test::UploadedFile:0x0055b544479128>" instead of the actual file. The same code works in controller tests 回答1: try to use fixture_file_upload : fixture_file_upload or if you wanted to

Stubbing RestClient response in RSpec

江枫思渺然 提交于 2020-01-01 08:27:49
问题 I have the following spec... describe "successful POST on /user/create" do it "should redirect to dashboard" do post '/user/create', { :name => "dave", :email => "dave@dave.com", :password => "another_pass" } last_response.should be_redirect follow_redirect! last_request.url.should == 'http://example.org/dave/dashboard' end end The post method on the Sinatra application makes a call to an external service using rest-client. I need to somehow stub the rest client call to send back canned

How do I get colour with Windows command prompt using RSpec in Ruby?

时间秒杀一切 提交于 2020-01-01 08:27:11
问题 In other o/s RSpec returns nicely coloured results (red, green etc). However in the windows (Vista) command prompt my text output is just plain old boring white. How can I bring colour to my RSpec test results? Thanks Evolve 回答1: UPDATE: Win32Console no longer works with rspec. ANSICON recommended. https://github.com/rspec/rspec-rails/issues/487#issuecomment-3556806 回答2: I had to install ansicon, and now everything is fine. (Even in my Aptana terminal). Install ansicon description: http:/