Parsing Enum in a table.CreateInstance fails

1,081 views
Skip to first unread message

Stanislaw Wozniak

unread,
Jan 24, 2011, 7:57:54 AM1/24/11
to SpecFlow
Hi,

I was trying to use auto-convert feature for enums which works fine in
the steps but fails to parse when used in a table.CreateInstance.

My step is:

And my search parameters are:
| Field | Value |
| term | google search |
| from | Today |
| until | Tomorrow |

my step:
[Given("my search parameters are:")]
public void GivenMySearchParametersAre(Table table) {
var searchParameters = table.CreateInstance<SearchParameters>();
}

And my SearchParameters class:

public class SearchParameters {
public string term { get; set; }
public DateEnum from { get; set; }
public DateEnum until { get; set; }
}

and finally my enum:

public enum DateEnum {
Today,
Tomorrow
}

Conversion fails because specflow is complaining that there are two
properties with the same enum type. Why would this not be allowed? I
think it's perfectly reasonable to have two properties in my table
mapped class with the same enum type.


// SPECFLOW CODE

public static Enum GetEnum<T>(this TableRow row, string id)
{
var value = row[id].Replace(" ", string.Empty);

var p = (from property in typeof(T).GetProperties()
where property.PropertyType.IsEnum &&

EnumValueIsDefinedCaseInsensitve(property.PropertyType, value)
select property.PropertyType).ToList();

if (p.Count == 1)
return Enum.Parse(p[0], value, true) as Enum;

if (p.Count == 0)
throw new InvalidOperationException(string.Format("No
enum with value {0} found in type {1}", value, typeof(T).Name));

throw new InvalidOperationException(string.Format("Found
sevral enums with the value {0} in type {1}", value, typeof(T).Name));
}

x97mdr

unread,
Jul 4, 2011, 11:26:30 AM7/4/11
to spec...@googlegroups.com
We just encountered the same problem on my team here.  Has this issue been resolved at all?

Stanisław Woźniak

unread,
Jul 5, 2011, 5:39:57 AM7/5/11
to spec...@googlegroups.com
This has been fixed in the latest version of specflow. Get the source and build for yourself.

Kind regards,
Stanislaw Wozniak

Stanisław Woźniak

unread,
Jul 5, 2011, 5:44:49 AM7/5/11
to spec...@googlegroups.com
Actually, when I think of it, I think the fixes are part of 1.6.1, but I might be wrong.

Kind regards,
Stanislaw Wozniak


2011/7/5 Stanisław Woźniak <st...@wozniak.com>

Darren Cauthon

unread,
Jul 5, 2011, 7:49:40 AM7/5/11
to SpecFlow

Hi x97mdr,

Stanislaw is right, the problem has been fixed in the source. If you
pull the source and build the TechTalk.SpecFlow project, you'll have a
dll that will work for you. The fix will be included in the next
release.

I just verified the example above with the latest source, and I can
verify that the problem is resolved. However, I checked it against
the 1.6.1 build (the latest on NuGet), and the fix is *NOT* on
1.6.1.

I'm sorry for the confusion!



Darren

On Jul 5, 4:44 am, Stanisław Woźniak <s...@wozniak.com> wrote:
> Actually, when I think of it, I think the fixes are part of 1.6.1, but I
> might be wrong.
>
> Kind regards,
> Stanislaw Wozniak
>
> 2011/7/5 Stanisław Woźniak <s...@wozniak.com>
>
>
>
>
>
>
>
> > This has been fixed in the latest version of specflow. Get the source and
> > build for yourself.
>
> > Kind regards,
> > Stanislaw Wozniak
>

Darren Cauthon

unread,
Jul 5, 2011, 10:02:52 AM7/5/11
to SpecFlow

Sorry, I thought I wrote a reply to this, but I guess it didn't get
sent...

There are a couple issues involved here:

1.) The enum issue that Stanislaw mentioned with CreateInstance has
been fixed. The fix is in the latest source, so you can pull it from
there and build if you need it right now. Otherwise, it will be in
the next release.

2.) Two properties with the same enum type: That issue was just
fixed by Marcus here: http://bit.ly/k4xKbK


None of these fixes are in the 1.6.1 release (which is the latest on
NuGet).

Sorry for the confusion!


Darren



On Jul 5, 4:44 am, Stanisław Woźniak <s...@wozniak.com> wrote:
> Actually, when I think of it, I think the fixes are part of 1.6.1, but I
> might be wrong.
>
> Kind regards,
> Stanislaw Wozniak
>
> 2011/7/5 Stanisław Woźniak <s...@wozniak.com>
>
>
>
>
>
>
>
> > This has been fixed in the latest version of specflow. Get the source and
> > build for yourself.
>
> > Kind regards,
> > Stanislaw Wozniak
>

Jeffrey Cameron

unread,
Jul 5, 2011, 8:46:08 PM7/5/11
to spec...@googlegroups.com
Awesome, thanks gang.  I was going to write a patch for it myself when I got a moment (no internet at work so a PITA to get source code).

2011/7/5 Darren Cauthon <darren...@gmail.com>
Reply all
Reply to author
Forward
0 new messages