【问题标题】:The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?)找不到类型或命名空间名称“Moq”(您是否缺少使用指令或装配参考?)
【发布时间】:2015-06-09 02:15:50
【问题描述】:

我确实使用包管理器控制台安装了 Moq (Install-Package Moq -version 4.1.1309.1617 -projectname EssentialTools.Tests)

我的目标是 .NET FRAMEWORK 4.5,(我确实尝试将其更改为 4 Client Profile),但仍然有错误。我在“使用起订量”下收到错误消息。任何帮助,将不胜感激。谢谢

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

【问题讨论】:

  • 在参考列表中,起订量参考的位置是否有一个黄色三角形?
  • @BrianMains 感谢您的评论。不,如果您在解决方案资源管理器中谈论参考,我在参考中看不到黄色三角形
  • 是的,对于那个项目。还要确保 Moq 在 bin 文件夹中。

标签: asp.net .net asp.net-mvc-4 frameworks moq


【解决方案1】:

右键单击引用并从已安装的包文件夹中添加 Moq。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-23
    • 2015-10-08
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2020-11-28
    相关资源
    最近更新 更多