Simple.Data extension with assertions
I am using this very nice mini ORM, Simple.Data, to set up a lot of test data quickly and easily. I would really like to expand it for assertions. For example, I would like to assert on count:
Db.MyTable.GetCount(); <- Returns a dynamic
So that I can more or less appreciate what you would do with FluentAssertions. It might look like this:
Db.MyTable.GetCount().ShouldBe(X);
But I find it very difficult to do this without losing the benefits of dynamics .
Does anyone have a hint as to how this could be done or if it is possible even within reasonable limits?
I am currently browsing the src on GitHub trying to find a way that I can do this locally and work around with impromptu to find a way.
Unfortunately, there is no happy answer to this question. Dynamic and extension methods do not mix, as Jon S and Eric L explain here: Extension method and dynamic object
The answer, as in this question, is to call the ShouldBe function as a static method:
AssertionExtensions.ShouldBe(Db.MyTable.GetCount(), 3);
or inline-casting the return value method to a known type:
((int)Db.MyTable.GetCount()).ShouldBe(3);
Or, as you research, use Impromptu to apply the interface to MyTable using the GetCount method. I assume you've seen my blog post on Simple.Data and Impromptu, but if you don't have one: http://blog.markrendle.net/2012/10/12/howto-dial-up-the-static- on-simple-data /
In the classes you create, why don't you create your own custom assertion class and create object classes that you inherit from them.
public class MyClass : MyCustomExceptionClass
{
}
This way it will be easier for you to test the methods the way you want.