Has any got experience of using AutoMapper in “Given” steps to compare values in a table with objects?

209 views
Skip to first unread message

Ian Ringrose

unread,
Jun 1, 2011, 7:58:29 AM6/1/11
to SpecFlow
We have lots of tables we need to compare with collections of objects,
some of these objects have “sub objects” that we wish to flatten in
the expected result table. We also sometimes need to map names.

AutoMapper seems to already do most name mapping we need.

Darren Cauthon

unread,
Jun 1, 2011, 9:12:54 AM6/1/11
to SpecFlow
Is this what you're looking for?

https://github.com/techtalk/SpecFlow/wiki/SpecFlow-Assist-Helpers

When it comes to flattening sub-objects, I've had more success not
flattening them in the results and just making another step
definition.

For example, if I have an Order object with Items, I would have a Then
clause like this:

Then an order should exist with the following information:
| OrderID | Total |
| 1234 | 23.45 |
| 7890 | 50.00 |
And the order of id '1234' should have the following items
| Sku | Quantity | Price |
| 1 | 1 | 23.45 |
And the order of id '7890' should have the following items
| Sku | Quantity | Price |
| 2 | 2 | 25.00 |

And running the comparisons for these things are one-line method calls
with SpecFlow, with no special data manipulation necessary.

I think Gherkin works best when you use it to describe your system as
clearly and as accurately as possible. If you have an object with
child objects, then that's what you have: An object with child
objects. If you just stick with that, with steps that identify one
thing (or one special aspect of a thing, like sub-objects), you'll
find that your steps are very reusable. When I've done special
manipulation, my steps have quickly become brittle. Or worse, I've
had to make "special" ones to handle different situations.

And just to further emphasize from my own experience: Sticky tables
are hell. They're hard to maneuver in the step definition. They
trick you into thinking you can change them when you can't. They are
the worst technical debts in that you don't even know you're in debt
until you're already over your head. Beware.


Darren Cauthon

Jeff Mallinger

unread,
Jun 3, 2011, 3:46:04 PM6/3/11
to SpecFlow
I was thinking that maybe putting a JSON literal into a multiline
string could solve this scenario, something like:

Then an order should exist with the following data:
'''
{
"order": {
"orderId": "1234",
"total": "23.45",
"items": {
"sku": "1", "quantity": "1", "price": "23.45",
}
},
"order": {
"orderId": "7890",
"total": "50.00",
"items": {
"sku": "2", "quantity": "2", "price": "25.00",
}
},

}
'''

... and deserialize using JSON.NET. Then you'd have 2 object graphs
you could compare using reflection (I've used http://comparenetobjects.codeplex.com/
in unit tests to do this).

Does anyone have experience w/ that approach? Conceptually it seems
like it could work fine, but I hadn't gotten around putting it into an
implementation yet.


Darren - what do you mean by 'sticky tables'? Is that a reference to
this type of approach?

mathias kluba

unread,
Jun 6, 2011, 4:37:04 AM6/6/11
to spec...@googlegroups.com
I already had this idea, but for unit tests (not acceptance tests) of course.
The problem is that I want to verify multiple fields on an object: if I split into several given/then steps, I have to keep in the context the object to test:
Then I have a result object
And its status is "success"
And its description is "OK"
etc.
So the steps are not independant and not very reusable, with a lot of duplication.
That's why I thought about an "automapper" framework from Specflow Tables, then from JSon.

I can use csv in some cases, for tabular data, if it's too long to write it on the scenario (so I can use scenario outlines with different files)

So I think that these details should not be in the gherkin but in an external file.

Ian Ringrose

unread,
Jun 7, 2011, 4:31:47 AM6/7/11
to SpecFlow
I agree “sub objects” that are logically child objects, (e.g. order
lines on an order) should be handles with another step definition.

However think of customer that has an “address” property, the address
property have fields like “Town”. I wish to be able to check the
customer’s address as well as Name using column headers like
AddressTown.
> > AutoMapper seems to already do most name mapping we need.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages