Cucumber feature failing when rendering template with confusing error - ruby-on-rails

I'm trying to make my tests pass, but cucumber is failing with a very confusing error, probably template related, that I'm not able to understand and fix.
Here is my Gemfile and Gemfile.lock:
https://gist.github.com/ngw/dada0c0658a9ef8b5573
You can assume that everything is on its latest version as of today, 28 March 2015, including rails, cucumber, ruby (2.2.0p0), database_cleaner, and so on.
The view that fails to render is a devise view that renders just fine on the browser.
Here is the exception raised:
Background: # features/users/sign_up.feature:6
Given I am not logged in # features/step_definitions/user_steps.rb:9
Scenario: User signs up with valid data # features/users/sign_up.feature:9
When I sign up with valid user data # features/step_definitions/user_steps.rb:13
undefined method `[]' for nil:NilClass
(in /Users/ngw/petswantpats/app/assets/stylesheets/application.css) (ActionView::Template::Error)
./app/views/layouts/application.html.haml:6:in `_app_views_layouts_application_html_haml___302582815389802583_70306431437360'
./features/step_definitions/user_steps.rb:6:in `sign_up'
./features/step_definitions/user_steps.rb:15:in `/^I sign up with valid user data$/'
./features/support/database_cleaner.rb:11:in `block in <top (required)>'
features/users/sign_up.feature:10:in `When I sign up with valid user data'
Then I should see a successful sign up message # features/step_definitions/user_steps.rb:18
Here are the incriminated views:
https://gist.github.com/ngw/95c0a0e6db86cfa0357f
Here is the content of application.css.scss:
https://gist.github.com/ngw/ab766e0cfb5167d2d76a
The failing step:
def sign_up
visit '/users/sign_up'
end
Is it something JS related? I'm using poltergeist with capybara, my env.rb has:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
If I use launchy to open it in the browser I have a white page (???) and no other usable information in my test.log...
Can someone help me on how to debug this? TIA

Related

Capybara-Webkit: page.should have_content() not implemented?

I have recently attempted to use:
Then(/^I should see "(.*?)"$/) do |arg1|
page.should have_content(arg1)
end
To query the page and see if a text exists on the page.
This worked fine with the default Capybara driver, but after setting the javascript driver to capybara-webkit and running the tests again I get:
undefined method `find_xpath' for #<Capybara::Webkit::Driver:0x007fa3f00152e8> (NoMethodError)
./features/step_definitions/customer_steps.rb:12:in `/^I should see "(.*?)"$/'
features/manage_customers.feature:10:in `Then I should see "ABC XYZ"'
I am using the javascript driver since I am also using AngularJS to populate my data.
My questions:
Are have_content() and page.has_content?() not implemented in capybara-webkit?
What could be the source of the problem?
Looks like there may be an issue with capybara-webkit: https://github.com/thoughtbot/capybara-webkit/issues/499
I added gem 'capybara', '2.0.3' to my gemfile for now and that seems to fix the issue.

testing rails+backbone application with rspec & capybara

I'm writing a tests for my app with listed gems. I couldn't find how to set Capybara to work with backbone (ajax in all)
example test:
require 'spec_helper' describe "Main" do describe "GET /" do
it "displays articles" do
Article.create!(title:'title',body:'<p>body text</p>')
visit '/'
page.should have_content('body text')
end
end
end
and output of the test:
Failures:
1) Main GET / displays articles
Failure/Error: page.should have_content('body text')
expected there to be text "body text" in "Loading..."
# ./spec/features/main_spec.rb:8:in `block (3 levels) in <top (required)>'
"Loading..." is the pre ajax text in my view template...
The point is that I don't want to use Jasmine at the moment for this app
It looks like you are running Capybara with the default Rack::Test driver, which means no JavaScript. Rack::Test loads your app in process and fakes a browser DOM behind the Capybara API.
You need to use a driver that runs your tests in a real Web browser. There are a few different options: a Selenium driver comes with Capybara and runs your tests in a real browser (normally Firefox), others are implemented in separate gems, such as Poltergeist which uses PhantomJS and the headless WebKit browser.
There are instructions on the Capybara readme about setting up the right driver. You can switch a group of tests to use a JavaScript supporting driver by adding the js option to a describe block, e.g.:
describe "Main with JavaScript", :js => true do
# ...
end

Using "Bundle Update" in Rails produced bazillion errors on test

I'm trudging through this deeply error-prone tutorial on Ruby on Rails located here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book.
I've been working through a section about testing using rspec. Now, the instructions that this tutorial provided created a whole host of errors (deprecations, array issues, etc.) that filled up my page. After rummaging the internet for several hours, I decided to follow several suggestions to update all my gems.
Having updated my gems and attempted to perform this very basic test (the default test really), I got this whole pile of error that I couldn't begin to understand. All I can say is "please help".
Thank you.
> bundle exec rspec spec/requests/static_pages_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
←[31mF←[0m
Failures:
1) StaticPages GET /static_pages works! (now write some real specs)
←[31mFailure/Error:←[0m ←[31mget static_pages_path←[0m
←[31mNameError:←[0m
←[31mundefined local variable or method `static_pages_path' for #<RSpec::
Core::ExampleGroup::Nested_1::Nested_1:0x5168040>←[0m
←[36m # ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.19901 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:5←[0m ←[36m# StaticPages GET /st
atic_pages works! (now write some real specs)←[0m
If you upgraded all your gems to the latest, then one issue you probably have is your newer capybara gem no longer looks for your tests in 'spec/requests'. That test needs to be in 'spec/features' now. If there is no 'spec/features' just create it.
Also, capybara will need this line in your 'spec/spec_helper.rb' if it isn't already:
require 'capybara/rspec'

Why is capybara not available in request specs?

Working on a new Rails 3.2.9 app with rspec and capybara.
I have the following in the Gemfile:
gem 'rspec-rails'
gem 'capybara'
and the following in spec/spec_helper.rb:
require 'rspec/rails'
require 'capybara/rspec'
and in spec/requests/asdf_spec.rb:
require 'spec_helper'
describe 'Asdf' do
describe "GET /asdfs" do
it "should list asdfs" do
visit asdfs_path
end
end
end
This test is failing:
Failure/Error: visit asdfs_path
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x007fa7b68961a0>
# ./spec/requests/asdfs_spec.rb:19:in `block (4 levels) in <top (required)>'
So it looks like Capybara isn't getting loaded. Ack, why not? I feel like I've done this exact same thing a dozen times before... probably blanking on something stupid.
So it was a capybara version 2 change. I found this:
http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
which explains:
Upon upgrading to capybara 2.0, capybara will not be available by
default in RSpec request specs. Instead, a new type of spec--the
feature spec--has been created for use with capybara.
To upgrade to capybara 2.0, you'll need to do a few things:
Upgrade rspec-rails to 2.12.0 or greater
Move any tests that use capybara from spec/requests to spec/features. Capybara
tests use the visit method and usually assert against page.
Just some additional info for anybody having the same problem with the Capybara upgrade to 2.x. Check out rspec-rails docs under the Upgrading to Capybara 2 section.
Basically, In order to use the Capybara DSL(page & visit) you must move your existing specs into the spec/features directory. So you can only use page & visit in acceptance tests. No more page & visit in controller and request specs. Only the rack-test DSL (get|post|put|delete|head/response.body) is allowed in controller and request specs.
This is not recommended but there is a way to keep your specs working as they are:
RSpec.configure do |c|
c.include Capybara::DSL, :example_group => {
:file_path => "spec/requests"
}
end
The docs state that if you go this route then you are overriding the intended behavior and you are taking a risk.
And definitely don't make this as a reason not to upgrade to Capybara 2.x. Feature specs are easy to get used to and are easy to read. feature is just an alias for describe, background is an alias for before, scenario for it, and given for let.
Hope this helps anyone confused by the new changes.
The issue is in capybara gem itself.
gem 'capybara', '1.1.2' solves this issue ( Version 2.0.x fails )

Cucumber/Capybara/Selenium-Webdriver error?

I've recently upgraded to Rails 3 and I'm trying to get my Cucumber tests to run.
When I run the tests, I'm getting the following error:
wrong number of arguments (2 for 1) (ArgumentError)
./features/step_definitions/user_steps.rb:24:in `/^I am logged in$/'
features/account.feature:8:in `Given I am logged in'
This test does nothing more than opening my login page. When I don't include the #javascript flag at the top of my feature file, it runs fine. When I do include it, I get the error and Selenium (I think) opens an instance of Firefox but nothing happens in the browser.
EDIT:
My I am logged in step looks like this:
Given /^I am logged in$/ do
#user = Factory(:user, :email => "cucumber#test.com")
#user.activate
visit path_to("the login page")
end
I tracked down a similar problem using pry.
[1] pry(#<Cucumber::Rails::World>)> step %{I go to login}
ArgumentError: wrong number of arguments (2 for 1)
from ~/.rvm/gems/ruby-1.8.7-p352/gems/multi_json-1.0.4/lib/multi_json/engines/json_common.rb:9:in 'parse'
Which was fixed with gem 'json', :require => 'json/pure' ahead of bson in my Gemfile.
Edit: Look like it was also necessary to force use of 1.9.2 (rvm --rvmrc --create 1.9.2).

Resources