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.
Related
I am trying to create a gem, but when trying to load a module in test I get the following error. I used "Configurable Ruby gems: Custom error messages and testing" to set environment variables from users and that's where most of the code is from.
1) Msg91sms::Configuration with configuration block returns the correct authkey
Failure/Error: raise Errors::Configuration, "Msg94 auth key missing!" unless #authkey
NameError:
uninitialized constant Msg91sms::Configuration::Errors
# ./lib/msg91sms/configuration.rb:10:in `authkey'
# ./spec/msg91sms/configuration_spec.rb:7:in `block (3 levels) in <top (required)>'
but as per the folder structure and everything this should be Msg91sms::Errors::Configuration. I put only one here even though all tests are failing due to improper module loading.
The gem with this error can be found here: https://github.com/flyingboy007/msg91sms/tree/development
bundle exec rspec will throw all errors.
It should be something with naming or improper loading. But I can't figure out.
After following the answer by #sergio, I am now getting this error:
1) Msg91sms::Configuration with configuration block returns the correct authkey
Failure/Error: raise ::Msg91sms::Errors::Configuration, "Msg91 auth key missing!" unless #authkey
NameError:
uninitialized constant Msg91sms::Errors
# ./lib/msg91sms/configuration.rb:10:in `authkey'
# ./spec/msg91sms/configuration_spec.rb:7:in `block (3 levels) in <top (required)>'
Could someone tell me what am doing wrong here?
raise Errors::Configuration, "Msg94 auth key missing!" unless #authkey
Use fully qualified name to help ruby lookup the class.
raise ::Msg91sms::Errors::Configuration, "Msg94 auth key missing!" unless #authkey
The folder structure is like Errors::Configuration but the error is showing like Configuration::Errors..Dont know why..
It's trying to find Errors::Configuration within Msg91sms::Configuration (the current scope at that point). But since there's no Msg91sms::Configuration::Errors, it fails with that message.
I use Nokogiri gem to parse website with 'each' and 'content' methods. It's complicated parser with several usings of those methods. For some reason at some point task fails with error. Undefined method 'content' for nillclass. And gives me references for all lanes I use those classes in. It's not regular fatal error, If I try to put result of those content it prints result needed but still crashes. Here is error I get:
rake aborted!
undefined method `content' for nil:NilClass
/mnt/shoppe/lib/tasks/parser.rake:50:in `block (6 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:49:in `block (5 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:47:in `block (4 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:27:in `block (3 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:12:in `block (2 levels) in <top (required)>'
I'm new to ruby and RoR. Maybe there some type of error which only crash program if some number of this errors reached? Couldn't find mention of something like this, most likely I don't know the key word to look for. So any help will be useful. Thank You.
Undefined method 'content' for nil:NilClass.
This error means, at some point in your rake task, when you call something.content, your something is nil.
To avoid this issue, you can call content using try:
something.try(:content)
That way, your program won't crash if some of the somethings are nil.
Usually this means that your input varies at some point. For example, you're parsing an array of nodes in a loop and trying to access get content from a link, something like this:
<div>test</div>
<div>test</div>
<div></div>
Note the third div. If you're expecting in your code that div always contains a link, your code will fail on the div without link, since link will be nil and trying to get content on nil gives you an error you have.
To workaround it you may use link.try(:content) instead of calling link.content directly.
I my main rails project I have mounted a engine of common code which does common tasks like currency conversion.
I can reference the methods in this common engine like this:
MyCommon::CurrencyHelper.convert_to_currency
This works fine in the project code, but does not seem to be working when I change the RSpec tests to point to the common methods.
The tests fail as it cannot find the common methods:
NoMethodError: undefined method `convert_to_currency' for MyCommon::CurrencyHelper:Module
./spec/helpers/application_helper_spec.rb:211:in `block (3 levels) in <top (required)>'
Do I need to do something else?
If by "I change the RSpec tests to point to the common methods" you mean that you are running the RSpec tests from engines/my_common directory (the engine's tests found in engines/my_common/spec) then you can find/reference your convert_to_currency method like this CurrencyHelper.convert_to_currency in your tests.
So for example, Stuff::BookingSlot.book_a_slot! (where Stuff is the engine) would be tested like this:
it 'book_a_slot! marks a slot as unavailable when a host is available' do
ai = FactoryGirl.create(:booking_slot, available: false, host_id: 1)
ai2 = ai.clone
ai2.update_attributes(available: true, host_id: 2)
BookingSlot.book_a_slot!
expect(ai2.reload.available).to eq false
end
Not quite sure what's going on here. I'm moving over some code from another project of mine and suddenly the same specs from before are generating errors in the new project. All the errors appear to revolve around calling the stub method. Here's an example test:
it "retrieves active workers from Redis" do
#monitor.should_receive(:monitor_running?).and_return(false)
REDIS.should_receive( :smembers ).with( 'leaderboard-workers' ).and_return( [] )
#monitor.perform
end
This works. However if I switch the first test line to this:
#monitor.stub(:monitor_running?).and_return(false)
I end up with the following error:
1) LeaderboardMonitor#perform retrieves active workers from Redis
Failure/Error: #monitor.stub(:monitor_running?).and_return(false)
Mocha::ExpectationError:
unexpected invocation: #<Mock:0x7fcc18c8bab8>.and_return(false)
satisfied expectations:
- allowed any number of times, not yet invoked: #<Mock:0x7fcc18c8bab8>.monitor_running?(any_parameters)
# ./spec/workers/leaderboards/leaderboard_monitor_spec.rb:58:in `block (3 levels) in <top (required)>'
I'm not quite sure what's going on here. Is this an issue with Mocha overriding the stub method? How do I work around this?!?!?
I don't know what version of mocha you are using. Have you tried something like
#monitor.expects(:monitor_running?).returns(false).at_least_once
Hello i am receiving this error:
spec/models/stores/persistent_spec.rb:6:in block (2 levels) in <top
(required)>': undefined methodexpect' for #
(NoMethodError)
Here is my setup. i have a class called Store located inside app/models
i have a class called Stores::Temporary < Store placed inside app/models/stores
I am trying to write tests for Temporary and they are failing with the error above
i have created temporary_spec.rb inside spec/models/stores and the code for it is the following:
require "spec_helper"
describe Stores::Temporary do
end
i am trying to write an expect to raise_error statement.
I have also tried 5.should == 5 which results with no available test being detected inside this file. I am using guard to autorun the tests, so changes in the files are being detected (works correctly) and tests are rerun but appear as blank.
So after struggling with this for 2 hours i restarted the PC and it was working without changing anything... One of those stupid errors you just need to sorta sleep-over and retry.