Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?)

I did install Moq using package manager console (Install-Package Moq -version 4.1.1309.1617 -projectname EssentialTools.Tests)

I am targeting .NET FRAMEWORK 4.5, (I did try to change it to 4 Client Profile), still had the error. I get an error under "using Moq". Any help would be appreciated. Thank you

 using EssentialTools.Models;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using Moq;
 using System.Linq;

 namespace EssentialTools.Tests
{
[TestClass]
public class UnitTest2
{

    private Product[] products = {
       new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
       new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
       new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
       new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
   };

    [TestMethod]
    public void Sum_Products_Correctly()
    {
        // arrange
        Mock<IDiscountHelper> mock = new Mock<IDiscountHelper>();
        mock.Setup(m => m.ApplyDiscount(It.IsAny<decimal>()))
            .Returns<decimal>(total => total);
        var target = new LinqValueCalculator(mock.Object);

        // act
        var result = target.ValueProducts(products);

        // assert
        Assert.AreEqual(products.Sum(e => e.Price), result);
    }

    private Product[] createProduct(decimal value)
    {
        return new[] { new Product { Price = value } };
    }

    [TestMethod]
    [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
    public void Pass_Through_Variable_Discounts()
    {
        // arrange
        Mock<IDiscountHelper> mock = new Mock<IDiscountHelper>();
        mock.Setup(m => m.ApplyDiscount(It.IsAny<decimal>()))
            .Returns<decimal>(total => total);
        mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v == 0)))
            .Throws<System.ArgumentOutOfRangeException>();
        mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v > 100)))
            .Returns<decimal>(total => (total * 0.9M));
        mock.Setup(m => m.ApplyDiscount(It.IsInRange<decimal>(10, 100,
             Range.Inclusive))).Returns<decimal>(total => total - 5);
        var target = new LinqValueCalculator(mock.Object);

        // act
        decimal FiveDollarDiscount = target.ValueProducts(createProduct(5));
        decimal TenDollarDiscount = target.ValueProducts(createProduct(10));
        decimal FiftyDollarDiscount = target.ValueProducts(createProduct(50));
        decimal HundredDollarDiscount = target.ValueProducts(createProduct(100));
        decimal FiveHundredDollarDiscount = target.ValueProducts(createProduct(500));

        // assert
        Assert.AreEqual(5, FiveDollarDiscount, "$5 Fail");
        Assert.AreEqual(5, TenDollarDiscount, "$10 Fail");
        Assert.AreEqual(45, FiftyDollarDiscount, "$50 Fail");
        Assert.AreEqual(95, HundredDollarDiscount, "$100 Fail");
        Assert.AreEqual(450, FiveHundredDollarDiscount, "$500 Fail");
        target.ValueProducts(createProduct(0));
    }
}
}
like image 740
user3508766 Avatar asked Apr 04 '15 00:04

user3508766


1 Answers

Right click on references and add Moq from installed package folder. Hope it helps!

like image 59
mashet Avatar answered Sep 20 '22 11:09

mashet