HTTP_REFERER for RSpec is missing

Currently got this error message after executing my RSpec tests:

ActionController::RedirectBackError:
 No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].

The error message and Stackoverflow tells you to set request.env[“HTTP_REFERER”]. I did that:

request.env["HTTP_REFERER"] = "/signin"

But that didn’t helped. Instead of that I set the HTTP_REFERRER directly in the post. Here is the snippet from my test code:

post "/sessions", {:session => {:email => user.email, :password => user.password}}, {"HTTPS" => "on", 'HTTP_REFERER' => '/signin'}

That fixed my problem.

undefined method `visit’ for RSpec with Capybara

I just started to write an acceptance test with capybara. I followed the code example on the GitHub Page and I got this odd error:

Failure/Error: visit 'http://127.0.0.1:3000/signin'
 NoMethodError:
 undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fda48e0f680>

I placed my test in “spec/requests”. After some research I found out that the new Capybara GEM expects the test to be in “spec/features”. After I moved my test file to the right directory it worked perfectly.

Integration Tests with RSpec

RSpec is a Test Framework for Ruby! And RSpec is just awesome! It is the best Test Framework I know. Add this 2 lines to your Gemfile to the test group and the development group.

gem 'rspec-rails', '2.8.0'
gem 'webrat', '0.7.3'

Webrat is a browser simulator. With RSpec and Webrat you can write integration tests and test the UI of your Web App!

Assume you have a Web App with Ruby on Rails and you want to test your Landing Page. So that you ensure that some elements are for sure there. You could write a RSpec for that. Here is an example.


require 'spec_helper'

describe "landing page" do

  it "diplays the landing page" do
    get "/"
    assert_response :success
    assert_select "form[action=?]", "/search"
    assert_select "input[name=?]", "q"
    assert_select "body div.container section"
    assert_select "h1", 2
  end

end

With “get /” you are basicly sending a request to the landing page. In the next line you just expect that you get a page successful back, without any HTTP 404 or 505 erors! In the next lines you ensure that there is a form on the landing page and the action of the form shows to “/search”. Further on you ensure that the input name in the form is “q”. And you ensure that there are 2 Header with h1 on the landing page.

If something of that is missing the test / spec will fail.

You can do much more with RSpec. This was just a small example. Check out the original page: http://rspec.info/

RSpeck before after

RSpec is a Test Framework for Ruby! And RSpec is just awesome! It is the best Test Framework I know. Add this 2 lines to your Gemfile to the test group and the development group.

gem 'rspec-rails', '2.8.0'
gem 'webrat', '0.7.3'

Webrat is a browser simulator. With RSpec and Webrat you can write integration tests and test the UI of your Web App!

Assume you want to write some specs for a class product. And you need for every spec to create a new product and after the spec to remove it. For this kind of tasks you can use before and after block in rspec.


before(:each) do
  @product = Product.new
end

after(:each) do
  @product.remove
end

So your rspec file maybe looks like this:


require 'spec_helper'

describe Product do

 before(:each) do
   @product = Product.new
 end

 after(:each) do
   @product.remove
 end

 describe "find_by_name" do

   it "returns an empty list. Search term is not in the DB" do
     name = "junitggasgagasgj8623"
     @product.name = name
     @product.prod_key = "gasgagasgj8623_junit/junit"
     @product.save
     results = Product.find_by_name( "sgj8623agajklnb8738gas" )
     results.should_not be_nil
     results.size.should eq(0)
   end

   it "returns an empty list. Search term is an empty string" do
     name = "junitggasgagasgj8623"
     @product.name = name
     @product.prod_key = "gasgagasgj8623_junit/junit"
     @product.save
     results = Product.find_by_name( "" )
     results.should_not be_nil
     results.size.should eq(0)
   end

  end

end

RSpec

RSpec is a Test Framework for Ruby! And RSpec is just awesome! It is the best Test Framework I know. Add this 2 lines to your Gemfile to the test group and the development group.

gem 'rspec-rails', '2.8.0'
gem 'webrat', '0.7.3'

Webrat is browser simulator. With RSpec and Webrat you can write integration tests and test the UI of your Web App!

Assume you have a simple class like that:

class Bowling
  def hit(pins)
  end

  def score
    0
  end
end

Than your RSpec Code would look like that:

require 'bowling'

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

In RSpec you don’t write tests, you write specifications. You describe a class and how it should behave! The name of a “test method” / “specification” is a regular string. I t can be a whole sentence or two!