tomas-stefano / api_matchers Goto Github PK
View Code? Open in Web Editor NEWCollection of RSpec matchers for APIs
License: MIT License
Collection of RSpec matchers for APIs
License: MIT License
The messages are all screw up:
expect(response.body).to have_json({})
Results in an strange error message:
Failure/Error: expect(response.body).to have_json({})
expect to have json: '{"orders":[]}'. Got: '{"orders"=>[]}'.
The objective is to show the expected and the actual like this:
Failure/Error: expect(response.body).to have_json({})
expect to have json: '{}'. Got: '{"orders"=>[]}'.
When you dealing with API's that return an certain amout of data, it becames near to impossible to see the diff between the expected and the actual.
ruby '2.1.2'
Rails: 4.0.8
When use the expect(response), expecting that the APIMatchers calls the 'body' method, the spec trows an exception.
Failure/Error: expect(response).to have_json({})
TypeError:
no implicit conversion of ActionController::TestResponse into String
# /Users/tomas-stefano/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/json/common.rb:155:in `initialize'
# /Users/tomas-stefano/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/json/common.rb:155:in `new'
# /Users/tomas-stefano/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/json/common.rb:155:in `parse'
# /Users/tomas-stefano/.rvm/gems/ruby-2.1.2@marketplace/gems/api_matchers-0.5.0/lib/api_matchers/core/parser.rb:5:in `json'
# /Users/tomas-stefano/.rvm/gems/ruby-2.1.2@marketplace/gems/api_matchers-0.5.0/lib/api_matchers/response_body/have_json.rb:14:in `matches?'
# /Users/tomas-stefano/.rvm/gems/ruby-2.1.2@marketplace/gems/rspec-expectations-2.14.5/lib/rspec/expectations/handler.rb:24:in `handle_matcher'
# /Users/tomas-stefano/.rvm/gems/ruby-2.1.2@marketplace/gems/rspec-expectations-2.14.5/lib/rspec/expectations/expectation_target.rb:34:in `to'
Change the readme to use the expect syntax
I execute a test like below:
is_expected.to have_node(:created_at).with(obj.created_at)
And the test pass when created_at is nil.
The problem is
if expected_value.present?
if expected_value is evaluated to a FalseClass, .present?
will still return false
1.9.2p320 :001 > false.present?
=> false
1.9.2p320 :002 > true.present?
=> true
1.9.2p320 :003 > nil.present?
=> false
1.9.2p320 :004 > false.nil?
=> false
1.9.2p320 :005 > true.nil?
=> false
1.9.2p320 :006 > nil.nil?
=> true
instead, use.nil?
to guard instead of .present?
Hey there.
One thing I just ran into while testing an api that returns a collection of resources wrapped in an array.
Per your example, this works fine:
"{ \"transaction\": { \"id\": \"54\", \"status\": \"paid\" } }".should have_node(:id)
=> true
However, this, when wrapped in an array, does not:
"[{ \"transaction\": { \"id\": \"54\", \"status\": \"paid\" } }]".should have_node(:id)
RSpec::Expectations::ExpectationNotMetError: expected to have node called: 'id'. Got: '[{ "transaction": { "id": "54", "status": "paid" } }]'
from /Users/joel/.rvm/gems/ruby-1.9.2-p320/gems/rspec-expectations-2.11.3/lib/rspec/expectations/fail_with.rb:33:in `fail_with'
Should that be the expected result? I would have expected it to find :id anyway.
The have_node(:foo).with is good but sometimes you need to inspect collections. Need to think and show a solution to the DSL.
Given response listing multiple records:
<messages>
<message><id>4</id></message>
<message><id>2</id></message>
</message>
response.body.should have_xml_node(:id).with(2)
will fail, even though it should not.
I tried some examples from the README and they fail for me. Gem Specs all pass against my Ruby and Rspec versions.
I simplified it to a 2 file test in a blank rvm gemset:
https://gist.github.com/3959775
Versions:
gem 'rspec', '2.11.0'
gem 'api_matchers', '0.1.1'
ruby 1.9.3p233 (2012-05-30 revision 35845) [x86_64-darwin11.4.0]
Failures:
1) APIMatcher examples shouldn't fail
Failure/Error: "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:transaction)
expected to have node called: 'transaction'. Got: '{ 'transaction': { 'id': '54', 'status': 'paid' } }'
# ./api_matchers_spec.rb:6:in `block (2 levels) in <top (required)>'
2) APIMatcher examples also shouldn't fail
Failure/Error: "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54)
expected to have node called: 'id' with value: '54'. Got: '{ 'transaction': { 'id': '54', 'status': 'paid' } }'
# ./api_matchers_spec.rb:10:in `block (2 levels) in <top (required)>'
You have body_method
but it should be response_body_method
in readme.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.