代码约定在单元测试时抛出MethodAccess异常

时间:2012-10-05 22:07:24

标签: unit-testing exception resharper code-contracts

我不太清楚为什么,但是当我启用运行时合同检查时,我在单元测试期间得到MethodAccessException。我使用 Machine.Specifications 测试框架和ReSharper / dotCover测试运行器。当我测试包含代码契约的程序集时,我得到了MethodAccessException

Machine.Specifications.SpecificationException Should be of type System.ArgumentNullException but is of type System.MethodAccessException
    at BusinessLogic.Specifications.When_testing_whether_a_set_of_cart_items_contains_a_product_and_a_null_set_is_passed.<.ctor>b__3()
    in CartItemQuerySpecs.cs: line 66

规范如下:

[Subject(typeof(ShoppingCartQueries), "Cart contains product")]
public class When_testing_whether_a_set_of_cart_items_contains_a_product_and_a_null_set_is_passed
    : with_fake_shopping_cart_repository
    {
    Establish context = () => QueryResult = CartItems.AsQueryable().ForUser(UserWithNoProductsInCart);
    Because of = () => Thrown = Catch.Exception(() => result = QueryResult.ContainsProduct(100));
    It should_throw = () => Thrown.ShouldBeOfType<ArgumentNullException>();
    static bool result;
    }

被测单位如下:

[Pure]
public static bool ContainsProduct(this IQueryable<CartItem> items, int id)
    {
    Contract.Requires<ArgumentNullException>(items != null);
    return (items.Any(item => item.ProductId.Equals(id)));
    }

为什么我会得到MethodAccessException

0 个答案:

没有答案