RSpec View testing with Rails3 and RSpec2 - ruby-on-rails-3

RSpec2 does not include an have_tag test helper. Using webrat's have_tag or have_selector matchers instead is not possible because Webrat and Rails 3 are not compatible yet. Is there a way to write useful RSpec view tests? It is possible to use assert_select instead of have_tag, but then one could Test::Unit tests in the first place. Or is it no longer recommendable to write RSpec view tests, because integration tests with Capybara or Cucumber are better?

Actually, Webrat works with Rails 3. I have tested this and I was able to use the have_selector matcher (have_tag didn't work).
You can take a look at this Google group discussion. Basically, you don't need the Webrat.configure block mentioned in the webrat readme, and following the mailing list solution, add these lines in your spec_helper.rb:
include Webrat::Methods
include Webrat::Matchers
As you can see, Webrat is not so updated anymore, so yes, you might be better off with integration testing with Cucumber (+ Capybara).

Webrat caused too much trouble, it is also possible to use Capybara with RSpec. The Capybara DSL (with the functions has_selector?, has_content?, etc.) is available for the following RSpec tests: spec/requests, spec/acceptance, or spec/integration.
If you use the latest version of Capybara (~> 1.0.1) - older versions like 0.4.0 won't support this - and add the following lines to your spec_helper.rb file
require "capybara/rspec"
require "capybara/rails"
then you could write for example the following RSpec request test
require 'spec_helper'
describe "Posts" do
describe "GET /blog" do
it "should get blog posts" do
get blog_path
response.status.should be(200)
response.body.should have_selector "div#blog_header"
response.body.should have_selector "div#blog_posts"
end
end
end

Related

Chapter 5 - Ruby on Rails Tutorial - undefined method `has_title?' when running RSpec tests

Just trying to get through the last bit of chapter 5 in Michael Hartl's Ruby on Rails Tutorial and getting another error running RSpec tests.
The output is :
Static pages should have the right links on the layout
Failure/Error: expect(page).to have_title('About Us')
NoMethodError:
undefined method has_title?' for #<Capybara::Session>
# ./spec/requests/static_pages_spec.rb:59:inblock (2 levels) in '
and results from the line starting expect(page) in the following code in static_pages_spec.rb :
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
end
Note : This happens running with or without Spork
Can anyone point me in the right direction please ?
Thanks,
Bazza
The have_title function is supported from Capybara 2.1. I suppose you have an older version of Capybara in your Gemfile. So, update your Gemfile with
gem 'capybara', '2.1.0'
then update Capybara like this
bundle update capybara
and rerun the specs using rspec. It should work now
Refer to this post for other options

Running multiple Rspec files in parallel with Selenium Grid

I have been working on a Rspec/Selenium Webdriver test framework where I need to run my Rspec tests distributed across multiple files in same directory spec/*_test.rb.I was looking for the existing solutions available and stumbled upon deep test gem (https://github.com/qxjit/deep-test) which helps driving the tests in parallel leveraging selenium grid but I was not able to implement it based on the documentation available and looks like there is no active development going on with it.Are there any ported version of deep test available to work with RSpec > 2.0.
I also looked into parallel_tests (https://github.com/grosser/parallel_tests) but not sure how we can use it for running multiple process on the same cpu with each process running a different rSpec test.
Here is a snippet from one of my spec file,
require 'selenium-webdriver'
require File.join(File.dirname(__FILE__),'../support/Setup')
require 'rspec'
require File.join(File.dirname(__FILE__),'../support/spec_helper')
require File.join(File.dirname(__FILE__),'../support/Helper')
describe "Test", :type => :selenium do
it "should search for flights" do
airline.home_page.queryFlight('oneWay', 'ATL', 'ORD', 'today')
end
it "should list all the flight results and select one flight" do
airline.flight_list_page.selectFlight
end
end
Similarly i have other spec files which I am trying to run in parallel.

Testing the asset pipeline with Capybara

I want to do simple request specs in my Rails 3.1 application with Capybara. The standard cases all work as expected, but when I want to test CSS generated by the asset pipeline, I receive the following error:
Failure/Error: visit '/assets/main.css'
ActionController::RoutingError:
No route matches [GET] "/assets/main.css"
I think the problem is that the test environment does not provide a complete server and so also no Sprockets middleware delivering the assets.
Is there a solution to this problem?
EDIT: Now possible!
We updated to Rails 3.2.12 and Capybara 2.0.2, now the assets are also available in the feature specs.
The Phusion guys blogged about a possibility to render an asset to a string:
MyApp::Application.assets.find_asset('main.css').body
You can also use this in tests. The solution is not ideal and/since Capybara isn't involved anymore, but it helps in my specific case to validate CSS. Better approaches are welcome!

no such file to load -- action_controller/integration - NoMethodError for Rails 3 and Webrat

I'm getting the following failure during RSpec tests..
no such file to load -- action_controller/integration
..using Rails 3, RSpec 2 and Webrat, if I include the Webrat helpers in the following way (the idea was to use Webrat for the have_tag and have_selector methods instead of assert_select).
RSpec.configure do |config|
..
config.include Webrat::HaveTagMatcher
end
Yet apparently Webrat and Rails 3 are not compatible yet. One solution is to avoid the Webrat gem and to use assert_select instead. Has anyone a better solution? How do you avoid the error?
I have not found a solution for this problem, only a work around. You can use the have_selector method of Capybara instead of Webrat, at least in RSpec request tests. There are certain difficulties with RSpec2 and Capybara (page.should have_selector only works if you use Capybara's 'visit' method, and not the RSpec method get '/some/path'), but basically it works.

Undefined Method error 'has_content?' with rspec/capybara/rails3

I am getting an undefined method 'has_content?' error on an rspec controller_spec file.
I found a thread with similar issues though that thread said the issue was fixed in rspec2.0beta (it was a fairly old thread) but I'm getting this with a more recent version. Some threads on rspec shows that capybara doesn't work in view specs, but I'm working in the controller specs so that shouldn't be the issue...
My Gemfile info looks like this:
rspec-rails+ dependecies 2.6.0.rc6
capybara 0.4.1.2
rails 3.0.7
I am trying to do a simple assert like
response.body.should have_content("Project A")
Thanks for the response,
Tony
Capybara is only included in Rspec request specs by default. Change this file to a be a request spec (put it in the request specs directory, change the title of it...)
Read the capybara Readme section 'Using Capybara with RSpec'
https://github.com/jnicklas/capybara
Also, if these are the types of asserts you're looking to do, this qualifies as a request spec more than it is a controller spec.