Assertion with Rspec

1

I use the Cucumber + Capybara + Ruby framework and I'm not able to make an assertion using the "expect" method.

I need to, for example, validate four messages returned in the body, but displays error:

Expected: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"

Obtained: "qui est esse"

#encoding: utf-8
Given(/^I send a get to see four titles$/) do
    @api = HTTParty.get("https://jsonplcaholder.typicode.com/posts")
end

Then(/^Returned status code "(.*?)"$/) do |statuscode|
    expect(@api.code.to_s).to eq statuscode
end

Then(^Returnered message "(.*?)"$/) do |message|
    expect(@api[0]["title"]).to eq message
    expect(@api[1]["title"]).to eq message
    expect(@api[2]["title"]).to eq message
    expect(@api[3]["title"]).to eq message
end


Feature: message body
@titles
Scenario: Display of title all films
Given I send a get to see four titles
Then Returned status code "200"
Then Returned message "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
Then Returned message "qui est esse"
Then Returned message "ea molestias quasi exercitationem repellat qui ipsa sit aut"
Then Returned message "eum et est occaecati"

compared using ==
 RSpec::Expectations::ExpectationNotMetError

    
asked by anonymous 19.09.2017 / 14:09

1 answer

1

So I understand you are trying to reuse the steps, you need to know if when the response comes back it will have several objects inside it.

This here:

Then(^Returnered message "(.*?)"$/) do |message|
  expect(@api[0]["title"]).to eq message
  expect(@api[1]["title"]).to eq message
  expect(@api[2]["title"]).to eq message
  expect(@api[3]["title"]).to eq message
end

This validates the body of the entire response, if in the second assertion you do not have the rest of the objects it will not work.

For best practice scenario writing you should not have more than one Then / So, because your scenario does not get atomic you end up validating more than one thing and it creates a lot of responsibility. Each scenario should validate only one thing, you could have scenarios that validate the other schema that validate the content, but never all together.

What is happening is when he enters the step he does not know who will validate and enters the flow again from position 0 he will try to give an expect and will not find who should.

You could have something along these lines:

Feature: message body
@status_code
Scenario: Display of title all films
Given I send a get to see four titles
Then Returned status code "200"

@titte
Scenario: Display of title all films
Given I send a get to see four titles
Then Returned message "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"

And your step should be a single validation:

Then(^Returnered message "(.*?)"$/) do |message|
  expect(@api[0]["title"]).to eq message
end

So you have less complexity and undock your tests.

I would recommend using rspec + httparty cucumber was not created for this type of testing but rather to create engagement about functionality and business rules.

    
26.03.2018 / 19:48