如何使用另一个类作为参数的构造函数来模拟一个Concrete类,在c#中使用Moq

时间:2018-05-04 16:34:30

标签: c# .net unit-testing moq xunit

我有测试购物车的代码。我试图模拟一个类(是的不是接口,在下面进一步),它在构造函数参数中采用另一个类。

测试类的构造函数是:

    public class CheckoutServiceShould
        {
            List<Product> _allMockedProducts; 
            Mock<InMemoryProductService> _mockInMemoryProductService;
            Mock<DiscountService> _discountService;
            Mock<ShoppingBasket> _mockShoppingBasket;


     public List<Product> GetAllProducts()
         {
             return new List<Product>
                {
                new Product { Id = 1, Name = "Apple", Code = "A", Price = new 
    Price { Amount = 100 , Currency = "GBP"}},
                new Product { Id = 2, Name = "Banana", Code = "B", Price = new 
    Price { Amount = 150, Currency = "GBP" }},
                new Product { Id = 3, Name = "Grapes", Code = "C", Price = new 
    Price { Amount = 300, Currency = "GBP" }}
            };
        }

        public CheckoutServiceShould()
        {
            _mockShoppingBasket = new Mock<ShoppingBasket>();
            _mockInMemoryProductService = new Mock<InMemoryProductService> 
   (_mockShoppingBasket.Object);
            _discountService = new Mock<DiscountService>();
            _allMockedProducts = GetAllProducts();
        }

我尝试运行测试时收到此消息(测试失败并显示错误) 测试失败: 按摩: 消息:Castle.DynamicProxy.InvalidProxyConstructorArgumentsException:无法实例化类的代理:CheckoutSystem.Service.InMemoryProductService。 找不到与给定参数匹配的构造函数: Castle.Proxies.ShoppingBasketProxy

0 个答案:

没有答案