Assertion with the Rspec

Asked

Viewed 243 times

1

I use the Cucumber + Capybara + Ruby framework and I am unable to make an assertion using the "expect" method.

I need for example to validate four messages returned in body, but it 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

arquivo backend11.rb

arquivo backend11.feature

1 answer

1

From what I understand you are trying to reuse the Steps, you need to know if when the Sponse 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 here validates the whole Sponse body, if in the second assertion you do not have the rest of the objects will not work.

For good practice writing scenarios should not have more than one Then/ So, because your scenario does not get atomic you end up validating more than one thing and create 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 again in the flow since the 0 position he will try to give an expect and will not find who should.

You could have something on that line:

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 on your step it should be a single validation:

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

So you maintain a lower complexity and decouple your tests.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.