Skip to content

Instantly share code, notes, and snippets.

@marcusoftnet
Created September 5, 2011 13:57
Show Gist options
  • Save marcusoftnet/1195044 to your computer and use it in GitHub Desktop.
Save marcusoftnet/1195044 to your computer and use it in GitHub Desktop.
SpecFlow ... using method binders... or what we should call them
[Binding]
public class DynamicStepArgumentTransformations
{
[StepArgumentTransformation]
public IEnumerable<object> TransformToEnumerable(Table table)
{
return table.CreateDynamicSet();
}
[StepArgumentTransformation]
public IList<object> TransformToList(Table table)
{
return table.CreateDynamicSet().ToList<object>();
}
[StepArgumentTransformation]
public dynamic TransformToDynamicInstance(Table table)
{
return table.CreateDynamicInstance();
}
}
[Binding]
public class StepArgumentTransformationSteps
{
[Given(@"I create a set of dynamic instances from this table using step argument transformation")]
public void a(IList<dynamic> dynamicSet)
{
State.OriginalSet = dynamicSet;
}
[When(@"I compare the set to this table using step argument transformation")]
public void b(Table table)
{
table.CompareToDynamicSet(State.OriginalSet);
}
[Given(@"I create a dynamic instance from this table using step argument transformation")]
public void c(dynamic instance)
{
State.OriginalInstance = instance;
}
[When(@"I compare it to this table using step argument transformation")]
public void d(Table table)
{
table.CompareToDynamicInstance((object)State.OriginalInstance);
}
}
@darrencauthon
Copy link

Yeah, it's definitely possible. I forgot what class was the driver for this, but there is a spot in the framework where filling the arguments on the step are managed. It would just be a matter of injecting in there, checking if the value is IEnumerable, and moving from there.

I took a look at the framework a few months ago to consider this and adding Guid argument support (doing the ES thing). I can't remember where it was, but I do remember thinking that it wasn't going to be as easy as I had hoped.

The only thing I might suggest is that you start with IEnumerable and support, and then move to IList and T[] from there.

I'm looking forward to you implementing this! You are volunteering, right?

@marcusoftnet
Copy link
Author

Yes - it the Step argument conversions attribute I think (https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions).
As far as implementing it.... I have some stress related (I think) issues to deal with first. Seeing someone to find out what it is soon. Then I'll get on this.

@darrencauthon
Copy link

Hope everything is ok. Sorry, I wasn't trying to push, I just think it's a good idea. :)

@marcusoftnet
Copy link
Author

marcusoftnet commented Sep 6, 2011 via email

@darrencauthon
Copy link

Just a suggestion, but I think the IList example should probably be built first, using the existing 3.5 functionality. That feature can be added to SpecFlow today, as is, without having to upgrade to 4.0.

Part of any new 4.0 branch can include the IEnumerable. Or better yet, branch from the 4.0 branch so it is not dependent on the IEnumerable feature.

@marcusoftnet
Copy link
Author

OK - either i totally failed to understand what is to be built or this was super-easy. The SpecFlow runtime had all the tools already. I simply used the StepArgumentTransformation.

I have a working version in the gist now.
It uses my DynamicExtension library (https://github.com/marcushammarberg/SpecFlow.Assist.Dynamic) for now but can easily be swaped to the main SpecFlow runtime when the dynamic stuff is moved there.

Also - to get this to work you have to use an configuration key - making the feature "opt-in" which might be nice i think.

I tried to get the transformation to work with the T (where T : class) but that threw on me. Probably some limitations in the current SpecFlow runtime.

What have i missed? What this what we talked about? Any ideas on the Transform thing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment