How can I test the page title with Capybara 2.0?

I had the same issues when I upgraded to Capybara 2.0, and managed to solve them by creating the following custom RSpec matcher using Capybara.string: spec/support/utilities.rb RSpec::Matchers::define :have_title do |text| match do |page| Capybara.string(page.body).has_selector?(‘title’, text: text) end end Now, in a spec file where subject { page }, I can use: it { should have_title(“My … Read more

Stubbing authentication in request spec

A request spec is a thin wrapper around ActionDispatch::IntegrationTest, which doesn’t work like controller specs (which wrap ActionController::TestCase). Even though there is a session method available, I don’t think it is supported (i.e. it’s probably there because a module that gets included for other utilities also includes that method). I’d recommend logging in by posting … Read more

When to use RSpec let()?

I always prefer let to an instance variable for a couple of reasons: Instance variables spring into existence when referenced. This means that if you fat finger the spelling of the instance variable, a new one will be created and initialized to nil, which can lead to subtle bugs and false positives. Since let creates … Read more

Why isn’t my CORS configuration causing the server to filter incoming requests? How can I make the server only accept requests from a specific origin?

CORS configuration won’t prevent the server from accepting requests based on the value of the Origin request header. You can’t do that just through CORS configuration. When you configure CORS support on a server, all that the server does differently is just to send the Access-Control-Allow-Origin response header and other CORS response headers. Actual enforcement … Read more