Table CreateInstance does not support nullable attributes / suggestions

978 views
Skip to first unread message

Großes mächtiges Männchen

unread,
Oct 16, 2010, 12:10:43 PM10/16/10
to SpecFlow
Hi, when I have the following class

public class Person
{
DateTime? BirthDate { get; set; }
}

and a step with a table like
| Field | Value |
| BirthDate | 6/9/1966 |

and I use something like

var person = table.CreateInstance<Person>();

then the BirthDate remains null.

I could actually "fix" this issue by adding the missing types to the
GetTypeHandlersForFieldValuePairs - method. I realized then that if I
leave the value empty (if that makes sense anyway), the value is set
to the min value since LoadInstanceWithKeyValuePairs doesn't handle
empty values (different from LoadInstanceWithPropertyData).

So my first suggestion would be to add the support for nullable values
here.

My second suggestion would be to add support for addtional types like
double, TimeSpan and Enumerations

What do you think?

Darren Cauthon

unread,
Oct 16, 2010, 10:24:57 PM10/16/10
to SpecFlow

Hello,

Thank you for the response! I never came across those situations when
I coded the helpers, so they didn't make it in. Now you have, so I
made the following updates:

1.) Added support for handling nullable types in CreateInstance<T>.
2.) Added support for double for CreateInstance<T> and CreateSet<T>.

I made a pull request for these updates here:

http://github.com/techtalk/SpecFlow/pull/13

With regards to TimeSpan, I'm not sure how to best express a
TimeSpan. Handling things like int, DateTime, decimal, etc. are easy
because I can just take the string from the table and run Convert over
it. I do no interpretation of the values, so .Net does all the work.
But when it comes to things like TimeSpan and Enumerations (do you
mean CreateSet<T>?), there would probably have to be some
interpretation. I'm not sure that type of interpretation on
SpecFlow's part would be a good thing.

With these helper methods, the thing to keep in mind is that they are
meant to be *helpers* and nothing else. If you have a complex
property type or if you have something that the helper doesn't manage,
you might have to do the work manually. For example, if I had a step
like this:

And my account has these values:
| Field |
Value |
| FirstName |
Darren |
| LastName |
Cauthon |
| ThisIsAnEnumWhichIsNotCurrentlySupported | EnumValue1 |

You might have to use code like the following to get the values out:

var account = table.CreateInstance<Account>();
account.ThisIsAnEnumWhichIsNotCurrentlySupported = // pull the value
out of the table manually

In this case, FirstName and LastName are strings, but that third
property is one that won't be covered by CreateInstance<T>. It might
not seem optimal to have to do manual work like that *on top* of the
CreateInstance<T> call, but hey... what would you have done if the
helper method didn't exist? :)

But thanks again for passing these issues along, and please send any
other feedback as well!



Darren Cauthon


On Oct 16, 11:10 am, Großes mächtiges Männchen <alex_wink...@aon.at>
wrote:

Großes mächtiges Männchen

unread,
Oct 17, 2010, 7:13:22 AM10/17/10
to SpecFlow
Hi,

thanks for your quick reply and the requested change.

After looking in the code again and thinking how I could implement an
automatic enumeration I realized that in most cases it's easier to
implement that in the input models. So agreed, these are just helpers,
and nothing more easier than writing properties that convert from
strings to enumerations.

The similar is true for TimeSpan - which I guess is rarely used
anyway, I just need it for my current project, which does a few time
calculations. One could use the TimeSpan.Parse/TryParse instead of
Convert, but the supported format is not optimal anyway (d.h:m:s.ms).
I will also give this a second thought...

Cheers,
Alex

jbandi

unread,
Oct 17, 2010, 6:25:07 PM10/17/10
to SpecFlow
Thanks guys!
I merged the changes from Darren to master.
This will included in the next SpecFlow release.


On Oct 17, 1:13 pm, Großes mächtiges Männchen <alex_wink...@aon.at>

John Plummer

unread,
Nov 27, 2010, 1:31:36 PM11/27/10
to SpecFlow
Hi,
When I use table.CreateInstance<Person>() and the Person contains a
nullable datetime, the resulting instance has a null datetime rather
than the date specified in the table. All is working as expected with
table.CreateSet<Person>() though.

Not sure if this is a bug or me doing something daft.

Darren Cauthon

unread,
Nov 27, 2010, 11:49:12 PM11/27/10
to SpecFlow

Are you using the latest SpecFlow release, 1.4, or are you using the
latest pull from source? I wasn't able to reproduce the matter you
described (a nullable datetime not getting set), but it may have been
a matter that was resolved based on previous feedback.



Darren Cauthon
Reply all
Reply to author
Forward
0 new messages