Home > Net >  List<int>.Contains) may not be used in setup / verification expressions
List<int>.Contains) may not be used in setup / verification expressions

Time:04-28

I want Mock a method but my method is in Condition and I need the value be true and this operation make my test pass if I dont mock my method its give me null exception so it important this is my test

  [Test]
    public void Can_return_price_according_to_semester_status()
    {
        var product = new Product
        {
            ProductTypeId = 15,
            Id = 1,
            Name = "Product name 1",
            Price = 12.34M,
            CustomerEntersPrice = false,
            Published = true,
            PreRegistrationPrice = 10.99M
        };

        _productService.Setup(x => x.GetSemesterProductTypeIds(It.IsAny<int[]>())
        .Contains(product.ProductTypeId)).Returns(true);// this is my target mock



        var customer = new Customer();
        _priceCalcService.GetFinalPrice(product, customer, 0, false, 1).ShouldEqual(product.Price);
        _priceCalcService.GetFinalPrice(product, customer, 0, false, 1).ShouldEqual(product.PreRegistrationPrice);
    }

and GetFinalPrice :

    if (_productService.GetSemesterProductTypeIds().Contains(product.ProductTypeId))// this is target of my mock
            {
                if (enrollmentTypeId <= 0)
                {
                    if (product.SemesterStatus == SemesterStatus.PreRegistration)
                    {
                        enrollmentTypeId = (int)EnrollmentType.PreRegistered;
                    }
                }
                if (enrollmentTypeId == (int)EnrollmentType.PreRegistered)
                {
                    price = product.PreRegistrationPrice;
                }
            }// some operation after this

problem so if u see i need my condition have true value but i dont know how i send it in mock ?

CodePudding user response:

It would make more sense to mock the GetSemesterProductTypeIds() call than to mock the Contains() method.

As per your example, let GetSemesterProductTypeIds() return the product type id of your product to make the Contains() call result in true.

_productService
    .Setup(x => x.GetSemesterProductTypeIds(It.IsAny<int[]>())
    .Returns(new List<int> { 15 });
  • Related