README.markdown in api_matchers-0.5.1 vs README.markdown in api_matchers-0.6.0

- old
+ new

@@ -13,10 +13,11 @@ * `be_ok` * `create_resource` * `be_a_bad_request` * `be_unauthorized` +* `be_forbidden` * `be_internal_server_error` * `be_not_found` ## Other Matchers @@ -54,37 +55,37 @@ **The default that have_node will parse is JSON.** You can verify if node exists: ```ruby -'{ "transaction": { "id": 54, "status": "paid" } }'.should have_node(:transaction) +expect('{ "transaction": { "id": 54, "status": "paid" } }').to have_node(:transaction) ``` Or if node exist with a value: ```ruby -'{ "transaction": { "id": 54, "status": "paid" } }'.should have_node(:id).with(54) +expect('{ "transaction": { "id": 54, "status": "paid" } }').to have_node(:id).with(54) ``` ```ruby -'{ "error": "not_authorized" }'.should have_node(:error).with('not_authorized') +expect('{ "error": "not_authorized" }').to have_node(:error).with('not_authorized') ``` ```ruby -'{"parcels":1 }'.should have_node(:parcels).with(1) +expect('{"parcels":1 }').to have_node(:parcels).with(1) ``` To see the json node and see if include a text, you can do this: ```ruby -'{"error": "Transaction error: Name cant be blank"}'.should have_node(:error).including_text("Transaction error") +expect('{"error": "Transaction error: Name cant be blank"}').to have_node(:error).including_text("Transaction error") ``` You can verify boolean values too: ```ruby -'{"creditcard":true}'.should have_node(:creditcard).with(true) +expect('{"creditcard":true}').to have_node(:creditcard).with(true) ``` ### HAVE NODE Matcher Configuration You can configure if you want xml (JSON is the default): @@ -94,29 +95,29 @@ config.content_type = :xml end ``` ```ruby -'<transaction><id>200</id><status>paid</status></transaction>'.should have_node(:status) +expect('<transaction><id>200</id><status>paid</status></transaction>').to have_node(:status) ``` Using the `with` method: ```ruby -'<transaction><id>200</id><status>paid</status></transaction>'.should have_node(:status).with('paid') +expect('<transaction><id>200</id><status>paid</status></transaction>').to have_node(:status).with('paid') ``` Or you can use the `have_xml_node` matcher: ```ruby -"<error>Transaction error: Name can't be blank</error>".should have_xml_node(:error).with("Transaction error: Name can't be blank") +expect("<error>Transaction error: Name can't be blank</error>").to have_xml_node(:error).with("Transaction error: Name can't be blank") ``` To see the xml node and see if include a text, you can do this: ```ruby -"<error>Transaction error: Name can't be blank</error>".should have_xml_node(:error).including_text("Transaction error") +expect("<error>Transaction error: Name can't be blank</error>").to have_xml_node(:error).including_text("Transaction error") ``` **If you work with xml and json in the same API, check the have_json_node and have_xml_node matchers.** You can configure the name of the method and then you will be able to use *without* the **#body** method, for example: @@ -124,72 +125,80 @@ ```ruby APIMatchers.setup do |config| config.response_body_method = :body end -response.should have_node(:foo).with('bar') +expect(response).to have_node(:foo).with('bar') ``` Instead of: ```ruby -response.body.should have_node(:foo) +expect(response.body).to have_node(:foo) ``` ### Have JSON Node Matcher ```ruby -'{ "transaction": { "id": 54, "status": "paid" } }'.should have_json_node(:id).with(54) +expect('{ "transaction": { "id": 54, "status": "paid" } }').to have_json_node(:id).with(54) ``` ### Have XML Node Matcher ```ruby -"<product><name>gateway</name></product>".should have_xml_node(:name).with('gateway') +expect("<product><name>gateway</name></product>").to have_xml_node(:name).with('gateway') ``` ### Have JSON Matcher Sometimes, you want to compare the entire JSON structure: ```ruby -"['Foo', 'Bar', 'Baz']".should have_json(['Foo', 'Bar', 'Baz']) +expect("['Foo', 'Bar', 'Baz']").to have_json(['Foo', 'Bar', 'Baz']) ``` ### Create Resource Matcher This matchers see the HTTP STATUS CODE is equal to 201. ```ruby -response.status.should create_resource +expect(response.status).to create_resource ``` ### BAD REQUEST Matcher This BAD REQUEST is a matcher that see if the HTTP STATUS code is equal to 400. ```ruby -response.status.should be_a_bad_request -response.status.should be_bad_request +expect(response.status).to be_a_bad_request +expect(response.status).to be_bad_request ``` ### UNAUTHORIZED Matcher This UNAUTHORIZED is a matcher that see if the HTTP STATUS code is equal to 401. ```ruby -response.status.should be_unauthorized -response.body.should have_node(:message).with('Invalid Credentials') +expect(response.status).to be_unauthorized +expect(response.body).to have_node(:message).with('Invalid Credentials') ``` +### FORBIDDEN Matcher + +This is a matcher to see if the HTTP STATUS code is equal to 403. + +```ruby +expect(response.status).to be_forbidden +``` + ### INTERNAL SERVER ERROR Matcher This INTERNAL SERVER Error is a matcher that see if the HTTP STATUS code is equal to 500. ```ruby -response.status.should be_internal_server_error -response.body.should have_node(:message).with('An Internal Error Occurs in our precious app. :S') +expect(response.status).to be_internal_server_error +expect(response.body).to have_node(:message).with('An Internal Error Occurs in our precious app. :S') ``` ### HTTP STATUS CODE Configuration You can configure the name method to call the http status code: @@ -201,36 +210,37 @@ ``` Then you can use without call the **#status** method: ```ruby -response.should create_resource +expect(response).to create_resource ``` This configurations affects this matchers: * `be_ok` * `create_resource` * `be_a_bad_request` * `be_internal_server_error` * `be_unauthorized` +* `be_forbidden` * `be_not_found` ### Be in XML Matcher This is a matcher that see if the content type is xml: ```ruby -response.headers['Content-Type'].should be_in_xml +expect(response.headers['Content-Type']).to be_in_xml ``` ### Be in JSON Matcher This is a matcher that see if the content type is in JSON: ```ruby -response.headers['Content-Type'].should be_in_json +expect(response.headers['Content-Type']).to be_in_json ``` ### Headers Configuration You can configure the name method to call the headers and content type: @@ -243,11 +253,11 @@ ``` And then you will be able to use without call the **#headers** calling the **#['Content-Type']** method: ```ruby -response.should be_in_json -response.should be_in_xml +expect(response).to be_in_json +expect(response).to be_in_xml ``` ### Acknowlegments * Special thanks to Daniel Konishi to contribute in the product that I extracted the matchers to this gem.