How to ignore or skip a test method using RSpec?

You can use pending() or change it to xit or wrap assert in pending block for wait implementation: describe ‘Automation System’ do # some code here it ‘Test01’ do pending(“is implemented but waiting”) end it ‘Test02’ do # or without message pending end pending do “string”.reverse.should == “gnirts” end xit ‘Test03’ do true.should be(true) end … Read more

Rspec: expect vs expect with block – what’s the difference?

As has been mentioned: expect(4).to eq(4) This is specifically testing the value that you’ve sent in as the parameter to the method. When you’re trying to test for raised errors when you do the same thing: expect(raise “fail!”).to raise_error Your argument is evaluated immediately and that exception will be thrown and your test will blow … Read more

POSTing raw JSON data with Rails 3.2.11 and RSpec

As far as I have been able to tell, sending raw POST data is no longer possible within a controller spec. However, it can be done pretty easily in a request spec: describe “Example”, :type => :request do params = { token: 0 } post “/user/reset_password”, params.to_json, { ‘CONTENT_TYPE’ => ‘application/json’, ‘ACCEPT’ => ‘application/json’ } … Read more

How to include Rails Helpers on RSpec

I normally include this code to require everything under my spec/support subdirectory once the Rails stack is available: Spork.prefork do # … Dir[Rails.root.join(‘spec’, ‘support’, ‘**’, ‘*.rb’)].each { |f| require f } RSpec.configure do |config| config.include MyCustomHelper # … end end Note that this will include MyCustomHelper in all example types (controllers, models, views, helpers, etc.). … Read more

Testing STDOUT output in Rspec

RSpec 3.0+ RSpec 3.0 added a new output matcher for this purpose: expect { my_method }.to output(“my message”).to_stdout expect { my_method }.to output(“my error”).to_stderr Minitest Minitest also has something called capture_io: out, err = capture_io do my_method end assert_equals “my message”, out assert_equals “my error”, err RSpec < 3.0 (and others) For RSpec < 3.0 … Read more