Search code examples
c#assertionssimple.datafluent-assertions

Extending Simple.Data with assertions


I'm using this very nice mini ORM, Simple.Data, to setup a lot of test data, quick and easy. I would really like to extend it for assertions. For example i would like to assert on count:

Db.MyTable.GetCount(); <- Returns a dynamic

So that I could evaluate more or less like you would do with FluentAssertions. It could look like this:

Db.MyTable.GetCount().ShouldBe(X);

But I'm finding it very hard to do this without loosing the advantage of dynamics.

Does anyone have a hint of how this could be done or if its even possible within reason?

I'm currently traversing the src at GitHub trying to find a way i can do this locally and toying around with impromptu to find a way.


Solution

  • Sadly, there is no happy answer to this. Dynamic and extension methods do not mix, as explained by Jon S and Eric L here: Extension method and dynamic object

    The answer, as in that question, is either to invoke ShouldBe as a static method:

    AssertionExtensions.ShouldBe(Db.MyTable.GetCount(), 3);
    

    or to inline-cast the method's return value to the known type:

    ((int)Db.MyTable.GetCount()).ShouldBe(3);
    

    Or, as you are investigating, to use Impromptu to apply an interface to MyTable with a GetCount method. I'm guessing you've seen my blog post on Simple.Data and Impromptu, but if you haven't: http://blog.markrendle.net/2012/10/12/howto-dial-up-the-static-on-simple-data/