Twitter Updates for 2009-06-25
June 25th, 2009Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Anonymous objects in C# are very handy, especially given the way they are supported by the ASP.NET MVC framework.
I recently ran into a case where I wanted to interact with an anonymous object. Specifically, I was testing the data that I provided a JsonResult. I handed the JsonResult a pretty complicated anonymous object with several layers of nesting. This is a great use of anonymous objects because in code they look a lot like JSON. So, how do I make sure that the JsonResult is getting the correct data? The answer is reflection. But like with all things, there is a hard way and an easy way.
First the hard way.
var example = new {
stringData = "string data",
integerData = 12,
booleanData = true
}
Given the block of code above, if we want to retrieve one of the values from the example instance we have to do the following.
string value = (string) example.GetType().
InvokeMember(
"stringData",
BindingFlags.GetProperty,
null,
example,
new object[] { });
Replace “stringData” with the name of any of the fields and there you go. The trouble is that this block of code is seven kinds of ugly (yes, I counted) and it is not the kind of thing that you want to type over and over. Wouldn’t it be nice if there was an easier way?
What if we use the following extension class. Also available as a gist on my github account.
static class ObjectExtensions
{
public static T Property<t>(this object target, string name)
{
return (T)target.GetType().InvokeMember(
name,
BindingFlags.GetProperty,
null,
target,
new object[] { });
}
}
Now we can access fields from our sample class by writing the following code.
string value = example.Property<string>("stringData");
int otherValue = example.Property<int>("integerData");
bool yetAnother = example.Property<bool>("booleanData");
I think that this looks much better. I hope this helps you as much as it has helped me.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
I needed an MVC helper method that generated the same markup that the WebForms Menu control does. I am not sure that it is 100% complete, but you can take a look at my first cut. I welcome any improvements or suggestions.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Ever since seeing it demo at Agile 2008, I have fallen in love with cucumber. Yesterday, I posted the source code for CucumberTFS. Read on for more information about what it does.
I have been trying to introduce some Behavior Driven Development to the project that I am working on. We recently received a large batch of requirements/stories. Our goal was to develop a better way to communicate functionality with our testing team. I suggested using the cucumber given, when, then format to describe all of our scenarios.
We stored these scenarios in our Team Foundation Server (TFS) instance. This lets us track code changes against the scenarios, assign them to different people and anything else that can be done with a TFS work item. I got the idea to retrieve the scenarios from TFS and format them so that they could be run through cucumber. Enter CucumberTFS.
CucumberTFS’s first iteration attempted to wrap the call to cucumber directly. Given the problems I had with redirecting cucumber’s output so that it appeared to come from CucumberTFS, I decided to modify the tool to just generate a single feature file that contains the contents of all the scenarios in TFS.
The tool still needs some work before it can be used by the masses. I want to create a binary release with an installer, for example. And there needs to be more control over the name of the file that is generated and more control over the set of TFS work items that are retrieved.
So, if you are using TFS and want to integrate it with cucumber, then check out CucumberTFS. Head over to github, fork the project and make your own changes.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.
Powered by Twitter Tools.