I am following the Mhartl Ruby on Rails tutorial and beginning to use Rspec. I was wondering if there is a way to fix what I assume are end of line characters that are not properly encoded when outputting to cmd such as: ←[0m
C:\PROGRAMMING\RailsSites\sample_app>bundle exec rspec spec/requests/static_pages_spec.rb
←[32m.←[0m←[32m.←[0m←[31mF←[0m
Failures:
1) Static Pages About page should have the content 'About Us'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/about'←[0m
←[31mActionController::RoutingError:←[0m
←[31mNo route matches [GET] "/static_pages/about"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:20:in `block (3 levels) in <top (required)>'←[0m
Finished in 3.66 seconds
←[31m3 examples, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:19←[0m ←[36m# Static Pages About page should have the content 'About Us'←[0m
Those are the ANSI codes to colorize the text.
Pass the --no-color option when you run the rspec command to disable these codes. You can also add --no-color to your .rspec file to make it a default option for your project.
Try this:
ANSI Command Line Colors under Windows
It worked on my aged XP machine, and it worked on W7 for the blog author, it probably works for you as well. It zeroed my cmd history BTW, but it looks like its back after the first test.
Related
I'm using TestRails to log failures in our automated test suite. Example failures have output like this:
Failures:
1) can click on the Photos button after logging in
Failure/Error: login.signin_btn.click
NoMethodError:
undefined method `click' for nil:NilClass
# /Users/kkrzeminski/TestApp/Appium/Common/pages_helper.rb:36:in `click'
# ./test_1_spec.rb:9:in `block (2 levels) in <top (required)>'
I can grab the exception using example.exception in the after(:each) block in my spec_helper, as well as the backtrace, but what I'm really interested is that line beginning with Failure/Error:. I can't seem to find a way to get that string. It would be handy for logging the reason for test failure in TestRails, as just the exception and the backtrace aren't very descriptive.
It sounds like what you really want is a custom formatter for your CI environment. The protocol for RSpec formatters provides an example_failed hook which will give you the example object.
You can then use Example and Notifications objects to gather the information you are desire.
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
I am upgrading from rails 3.2.19 to rails 4.1.5. I am using rspec-rails 2.14.0.rc1.
With rails 4.1.5 all my tests are passing, except for a handful that use stub. The ones that are failing are of the form:
ENV.stub(:[]).with("ADWORDS_RUN").and_return("Yes")
Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
Kernel.stub(:rand).and_return(2)
Each is returning ArgumentError: wrong number of arguments (1 for 2+). All were passing in rails 3.2.19. I have tried going back to rspec-rails 2.8.1, but same error. Also rails 4.0, but the error persists. The last error (stubbing :rand) does not occur when I run the whole test suite but does occur when I run the individual test file for that test. Here is an example test
it "should have google tracking code in production" do
Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
get :home
response.body.should =~ /Google Analytics Tracking code/
end
and here is the output from the test:
Failure/Error: Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
ArgumentError:
wrong number of arguments (1 for 2+)
# ./spec/controllers/pages_controller_spec.rb:107:in `block (4 levels) in <top (required)>'
Line 107 is the Rails.stub line.
Please let me know how to rectify this problem?
I did not manage to find the cause of this problem, but I did find a workaround. It was to change all the stub statements to the newer rspec allow syntax. So for example
Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
becomes
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production"))
I did this using rspec-rails 2.14.0.rc1. This fixed the problem.
Thank you for the comments, they either clarified my question or pointed me in the right direction.
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'
Does anyone know why I get the following error when I use the web_step#follow method?
When I follow "Stuff" within "#main-nav" # features/step_definitions/web_steps.rb:33
undefined local variable or method `node' for #<Capybara::Driver::RackTest::Node:0x00000101409b40> (NameError)
./features/step_definitions/web_steps.rb:35:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `block in with_scope'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:34:in `/^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/'
This is the html output:
<ul id='main-nav'>
<li>Things</li>
<li>Stuff</li>
</ul>
P.S. I have removed webrat and am solely using capybara
Thanks in advance!
per: https://github.com/jnicklas/capybara/issues/110
comment out this line in env.rb:
require 'cucumber/rails/capybara_javascript_emulation'
Note: after commenting that line you'll have to explicitly tag your features/scenarios with #javascript if you want to click links with onclick javascript handlers.
See also: https://github.com/aslakhellesoy/cucumber-rails/issues/77 which eventually takes you on a journey to discover it should be fixed in cucumber-rails v0.4.0 (2011-03-20). This may still be relevant for folks with Rails 2.3.x projects using cucumber-rails v0.3.2
This means that the actual output of your page does not include the element you're trying to search for. For example, if you had with_scope("#my_div") but your content didn't have any divs with the id my_div it would raise this exception.
I'd suggest trying to add a cucumber step of Then show me the page before the failing step, and investigate the source of the generated page.