3

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.

Christian Mikkelsen
  • 1,661
  • 2
  • 19
  • 44
  • I think the question needs to make clear that as far as the compiler is concerned, the return value from GetCount() is dynamic, so extension methods can't be resolved. – Mark Rendle Jan 25 '13 at 12:49

2 Answers2

2

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/

Community
  • 1
  • 1
Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
  • Thank you and yes i've read about impromptu on your blog. Inline casting seems to be the way to go then. I wonder why inline casting didn't it even cross my mind... Thank you for a great product by the way :). – Christian Mikkelsen Jan 25 '13 at 13:36
0

In the classes you are creating why dont you create your own custom assertion class and make the object classes you are creating inherit from them.

public class MyClass : MyCustomExceptionClass
{

}

In that way it would be easier for you to test the methods in the way you want

befree2j
  • 361
  • 5
  • 11