harsha
harsha

Reputation: 963

cucumber multiple assertions in a step

I am trying to validate one block of json data that I receive from server

json consists of information about bunch of orders. An each order includes cost of each part, taxes and total. And it is kind of strict requirement each order contains exactly 4 parts. And each order has three kind of taxes and a total.

I have a step which looks like this

   And "standardorder" includes parts "1..4", taxes "1..3" and total

and step implementation is like following. Here @jsonhelper.json is shared state (json for one order) passed from previous step.

 And /^"([^"]*)" includes parts "([^"]*)", taxes "([^"]*)" and total$/ do |arg1, arg2, arg3|
    json = @jsonhelper.json
    validkeys = ["total"]

    parts = arg2.split('..').map{|d| Integer(d)}
    (parts[0]..parts[1]).each do |i|
        validkeys.push "p#{i}"
    end

    taxes = arg3.split('..').map{|d| Integer(d)}
    (taxes[0]..taxes[1]).each do |i|
        validkeys.push "t#{i}"
    end

    validkeys.each do |key|
        json[arg1].keys.include?(key).should be_true
    end 
end

Now this script works fine except that if any one key is missing it doesn't state which one is missing. Either it passes or fails as assertions are iterated for each key.

I would like to know if there is any possibility of sending keys which are found ok to result stream. Thus my intention is to know to which keys are ok and which failed and which one skipped. As such order of keys is not expected in json.

Thanks in advance.

Upvotes: 1

Views: 3598

Answers (1)

Andy Waite
Andy Waite

Reputation: 11076

It's probably best to split the step definitions first:

 And "standardorder" should be received
 And the order should include parts 1 to 4
 And the order should include taxes 1 to 3
 And the order should include the total 

Then you can re-use the steps elsewhere.

The 'order' check easy to implement as you're just checking one element.

For the other two, you are really just checking the presence of items in an array, e.g.:

actual_values.should == expected_values

If that fails, RSpec will give you a report showing how the arrays differ.

Upvotes: 2

Related Questions