0

I'm not quite sure why, but when I enable runtime contract checking, I'm getting a MethodAccessException during unit testing. I use the Machine.Specifications test framework and the ReSharper/dotCover test runner. When I test my assembly containing code contracts, I get this 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

The specification looks like this:

[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;
    }

The unit under test looks like this:

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

Why would I get a MethodAccessException?

AakashM
  • 62,551
  • 17
  • 151
  • 186
Tim Long
  • 13,508
  • 19
  • 79
  • 147
  • 1
    You could write the MethodAccessException to a log file and analyze its message and stack trace. – thersch Oct 09 '12 at 08:55
  • If you have a complete repro, we can debug it and find a solution. What version of the tools are you using? – Manuel Fahndrich Apr 25 '13 at 16:52
  • I've just upgraded my tools to VS2012 update 2 and all the current versions of Code Contracts, and not had a chance to try to repro it yet with these tools. I will let you know if I can repro it. – Tim Long Apr 25 '13 at 20:50

0 Answers0