开发者

How could I unit test this?

开发者 https://www.devze.com 2023-01-09 05:40 出处:网络
I am using the Mono and Lokad quality libraries to develop an API whic开发者_StackOverflow社区h does things like inspect the characteristics of methods to see if they throw exceptions, etc etc.

I am using the Mono and Lokad quality libraries to develop an API whic开发者_StackOverflow社区h does things like inspect the characteristics of methods to see if they throw exceptions, etc etc.

One of my methods I have in this API looks like this:

// Get all methods which have a NotImplementedException
var throwingMethods = _codebase.Methods
    .Where(m => m.GetInstructions()
                 .Exists(i => i.Creates<NotImplementedException>())
    ).ToArray();
 return throwingMethods;

How could I unit test this? C# does not have its own native ability to do the same thing. Perhaps I could just have a small assembly, make a hard-coded collection, and compare?

Thanks


You could try mocking the _codebase variable to return some predefined methods for your unit test.

If _codebase is set by a variable passed to the constructor of the class you are testing (constructor injection) then this might be pretty easy (assuming of course that the Methods property is overridable and the class is not sealed).

0

精彩评论

暂无评论...
验证码 换一张
取 消