0

我不太清楚为什么,但是当我启用运行时合同检查时,我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

4

0 回答 0