How to get rspec-2 to give the full trace associated with a test failure?
You must run rspec with -b
option to see full backtraces
How can you get rspec to print failed test backtraces *as* it is running?
You have two options:
1) fail fast
# spec/spec_helper.rb
RSpec.configure do |c|
c.fail_fast = true
end
..or use it from the command line
$ bundle exec rspec spec/ --fail-fast
.F
Failures:
1) Swinger should set the Capybara driver
Failure/Error: Capybara.current_driver.should_not == :rack_test
Finished in 0.00479 seconds
2 examples, 1 failure
Basically this option on error will stop the test suite and it will print the error.
2) use rspec-instafail gem
https://github.com/grosser/rspec-instafail
This gem will show failing spec instantly and it will continue running specs.
Example.txt file not getting updated if test is failed in before(:all) in Rspec
There was a exit present in after(:all) method in my code that is the reason example.txt was not getting updated
Rspec: Failure/Error: expect(qbase).to have_received(:with_user_id).with(1)
Solved with Dependency Injection
If there is a better solution, please post it.
Search Query
class Search
def self.call(current_user, params, query_object_override = nil)
new(current_user, params, query_object_override).call
end
def initialize(current_user, params, query_object_override = nil)
@params = params
@current_user = current_user
@query_object = query_object_override
end
def call
base_filters(query_object).relation
end
private
def query_object
@query_object ||= CarQuery.new(@current_user)
end
def base_filters(query_object)
query_object.with_user_id(@params[:user_id]) if @params[:user_id].present?
query_object
end
end
Rspec Test
RSpec.describe Search, type: :model do
describe "#base_filters" do
it "should call with_user_id method on query_object when user_id is passed in" do
current_user = create(:user)
query_object = spy('CarQuery')
Search.call(current_user, {user_id: 1}, query_object)
expect(query_object).to have_received(:with_user_id )
end
end
end
Why are my view specs behaving inconsistently?
This is not a definitive answer: it appears that, for some weird reason, the evaluate
method in the Nokogiri's Searchable
module considers the concat()
function as a custom XPath function instead of the internal XPath function.
External XPath functions are called as normal ruby methods in the context of the handler
attribute of evaluate
. Normally the handler is the SubstitutionContext
class but in your case it seems that sometimes this gets the context of the TextHelper
module where the concat
method is defined, accepting just 1 parameter (whereas the XPath's concat()
function accepts any number of parameters). I think that this can lead to the errors you observe.
Could you open the searchable.rb
file (see the stack trace for its location) on this line and add before it some debug messages to inspect the contents of the following variables?
puts "ctx #{ctx.inspect}"
puts "path #{path.inspect}"
puts "handler #{handler.inspect}"
if handler.respond_to?(:concat)
puts "concat #{handler.method(:concat).inspect}"
else
puts "not responding to :concat"
end
Then, can you provide the output printed for one of the failing tests?
Update: the output showed that indeed the tests context was being polluted by the ActionView::TextHelpers
module and in the end it turned out that there was a ActionView::Helpers
module included in a model used in the tests.
What is wrong with this RSpec expectation?
You must not put a space between expect
and the opening paren (
.
The working code sample is as follows:
describe 'My code' do
it 'should work' do
expect( nil ).to be_nil
expect( "test" ).to eq( "test")
end
end
Related Topics
Ruby on Rails Advanced JSON Serialization
Given the Session Key and Secret, How to Decrypt Rails Cookies
Empty Attribute with Ruby Haml
How to Replace the Unicode Gem on Ruby 1.9
Ruby Rails - Select Only Few Columns from the Data Base
How to Merge Two Hashes with No New Keys
Ruby - Iterate Over Parsed JSON
How to Avoid Deprecation Messages from Rubygems
Executing User-Supplied Ruby Code on a Web Server
Rails 3 Additional Session Configuration Options (Key, Expires_After, Secure)
In Ruby, How to Check If Method "Foo=()" Is Defined