RSpec Tips

Discussion


The following tips are the result of searching for solutions for problems encountered during work. So these tips are tested solutions discussed in the online forum and blogs.

How to post XML data in controller spec


XML=<<-xml_string
<payment>
  <transaction-time>2012-05-16T21:02:08Z</transaction-time>
  <payment-amount>20000</payment-amount>
</payment>
xml_string
request.env['content_type'] = 'application/xml' 
request.env['RAW_POST_DATA'] =  some_long_xml_string 
post :create

Use VCR Webmock with RSpec


Step 1

Add the gems to the Gemfile under test group:

gem "webmock"
gem "vcr"

Step 2

bundle

Step 3

require 'vcr' 

as the first line in spec_helper.rb

Step 4

Add

VCR.configure do |c|
  c.cassette_library_dir = 'spec/fixtures/cassettes'
  c.hook_into :webmock
  c.ignore_localhost = false
  c.allow_http_connections_when_no_cassette = true
end

inside the configure block.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.