I understand that in order to do TDD, you must first write a failing test - red, write the logic and make it pass - green and then refactor if necessary. But the part what i was working on was quite simple. Which was just basically reading off data from sproc and then format the data using data model class and returning them via json.
How to do it?
So for such a simple type design, it would seem that tests aren't really needed. But i googled around and i did find people who wrote tests for their sproc and i wanted to write at least some basic tests to ensure that these apis are working. One of the method that i saw was to validate the data returned using an xml file to compare line by line.
I didn't like this method that much as i would need to create additional files, the xmls and not only that, it seems that the test are too fragile in case that the data in my database changed, i would have to go update and maintain the XML/test again. Not a very motivating thought.
So i choose a simpler method, using string contains. Basically what i did was to convert the json back into string and just search the string for certain attributes and value that should likely remain unchanged for quite some time and use those as my basic assertion.
Yea, i think the test are pretty much too basic and doesn't really do much. But at least they are useful to tell me when i have accidentally changed the structure too much until i am better able to figure out how to write a better test for this. :)
[Test]
[Category("GetProductJson")]
public void GetProductJson_CallBuggyBaby__ReturnFreeItemChunk()
{
//arrange
request.Setup(x => x.QueryString).Returns(
new System.Collections.Specialized.NameValueCollection
{
{"productid","12341"},
{"storeid","189}
}
);
//act
EStoreController controller = new EStoreController();
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
var results = controller.GetProductJson();
string json = JSONHelper.ToJSON(results.Data);
//assert
bool bHasFreeItem = json.Contains("\"nFreeItemID\":8888");
Assert.True(bHasFreeItem, "Json should contain free item.");
}


2 comments:
Iam following up on your question on stackoverflow.com. Did you ever get this resolved?
http://stackoverflow.com/questions/2726429/selenium-rc-selenium-testrunner-js-access-denied-error-on-ieproxy-help
WOuld you be able to share what you did if you got that resolved. Iam running into the same exact error.
update:
Iam using selenium RC 1.0.3 and using IE 8.0
Post a Comment