简体   繁体   English

单元测试项目坚持要求引用EntityFramework

[英]Unit Test project is insisting that it requires a reference to EntityFramework

I have an issue that just came up in a unit test project insisting that it requires a reference of EntityFramework, I am convinced it doesn't need it. 我有一个问题刚出现在一个单元测试项目中,坚持要求引用EntityFramework,我确信它不需要它。 Other projects are referencing the project/extension method that the unit test project is referencing/testing and using the extension method just fine without a reference to EntityFramework. 其他项目正在引用单元测试项目引用/测试的项目/扩展方法,并且使用扩展方法就好了而没有引用EntityFramework。

I have found that if I simply execute the extension method as a static method in the unit test project then the unit test project compiles just fine – just completely baffled. 我发现如果我只是在单元测试项目中将扩展方法作为静态方法执行,那么单元测试项目编译得很好 - 只是完全困惑。 I did not see anything informative in the build output. 我没有在构建输出中看到任何信息。

This does not compile: 这不编译:

[TestMethod]
public void BuildsEmptyRadioButtonList()
{
    var htmlHelper = Creator.GetHelper();

    var radioButtonList = htmlHelper.RadioButtonList("RadioGaga", new SelectListItem[0]);

    var expected = MvcHtmlString.Create(@"...");
    Assert.AreEqual(expected.ToHtmlString(), radioButtonList.ToHtmlString());
}

Build output: 构建输出:

1>------ Build started: Project: HA.Shared.Utilities.Mvc.Tests, Configuration: Debug Any CPU ------
1>C:\hatfs\Web2014\4-Test\Source\HA.Shared.Utilities.Mvc.Tests\HtmlHelperRadioExtensionsTests.cs(25,17,25,20): error CS0012: The type 'System.Data.Entity.IDbSet`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
========== Build: 0 succeeded, 1 failed, 8 up-to-date, 0 skipped ==========

The error is pointing to the “var” in the line that starts with “var radioButtonList”, I tried changing the “var” to “IHtmlString” with no change. 该错误指向以“var radioButtonList”开头的行中的“var”,我尝试将“var”更改为“IHtmlString”而没有任何更改。

This does compile: 这确实编译:

[TestMethod]
public void BuildsEmptyRadioButtonList()
{
    var htmlHelper = Creator.GetHelper();

    var radioButtonList = HtmlHelperRadioExtensions.RadioButtonList(htmlHelper, "RadioGaga", new SelectListItem[0]);

    var expected = MvcHtmlString.Create(@"...");
    Assert.AreEqual(expected.ToHtmlString(), radioButtonList.ToHtmlString());
}

Build output: 构建输出:

1>------ Build started: Project: HA.Shared.Utilities.Mvc.Tests, Configuration: Debug Any CPU ------
1>  HA.Shared.Utilities.Mvc.Tests -> C:\hatfs\Web2014\4-Test\Source\HA.Shared.Utilities.Mvc.Tests\bin\Debug\HA.Shared.Utilities.Mvc.Tests.dll
========== Build: 1 succeeded, 0 failed, 8 up-to-date, 0 skipped ==========

The signature of the RadioButtonList method is: public static MvcHtmlString RadioButtonList( this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> listItems, object radioButtonHtmlAttributes = null, object labelHtmlAttributes = null, bool vertical = false) RadioButtonList方法的签名是: public static MvcHtmlString RadioButtonList( this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> listItems, object radioButtonHtmlAttributes = null, object labelHtmlAttributes = null, bool vertical = false)

I just figured out the issue. 我刚刚弄明白了这个问题。 I recently added an extension method ReturnsSet (below), to more easily mock IDbSet s off of DbContext s in a shared unit test "helpers" project. 我最近添加的扩展方法ReturnsSet (下),能够更容易地嘲笑IDbSet小号断DbContext在共享单元测试“助手”项目秒。 Though I do not know why, apparently the compiler still felt that it needed a reference to EntityFramework to compile the specific project where the issue arose. 虽然我不知道为什么,显然编译器仍然认为它需要引用EntityFramework来编译出现问题的特定项目。 When I comment out the new ReturnsSet extension method, the specific unit test project compiles using the extension method form of syntax. 当我注释掉新的ReturnsSet扩展方法时,特定的单元测试项目使用扩展方法形式的语法进行编译。

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;

using Moq;
using Moq.Language.Flow;

public static class DbMockHelpers
{
    public static Mock<IDbSet<TEntity>> MockSingle<TEntity>(TEntity data) 
        where TEntity : class, new()
    {
        return MockDbSet(new[] { data });
    }

    public static Mock<IDbSet<TEntity>> MockDbSet<TEntity>(params TEntity[] data)
        where TEntity : class, new()
    {
        return MockDbSet(data.AsEnumerable());
    }

    public static Mock<IDbSet<TEntity>> MockDbSet<TEntity>(IEnumerable<TEntity> data) 
        where TEntity : class, new()
    {
        var list = data == null ? new List<TEntity>() : data.ToList();
        var observable = new ObservableCollection<TEntity>(list);
        var dbSet = new Mock<IDbSet<TEntity>>();
        dbSet.Setup(d => d.Add(It.IsAny<TEntity>())).Callback((TEntity entity) => list.Add(entity));
        dbSet.Setup(d => d.Remove(It.IsAny<TEntity>())).Callback((TEntity entity) => list.Remove(entity));
        dbSet.Setup(d => d.Attach(It.IsAny<TEntity>())).Callback((TEntity entity) => list.Add(entity));
        dbSet.Setup(d => d.Create()).Returns(new TEntity());
        dbSet.Setup(d => d.GetEnumerator()).Returns(() => list.GetEnumerator());
        dbSet.Setup(d => d.Local).Returns(observable);
        dbSet.Setup(d => d.ElementType).Returns(typeof(TEntity));
        dbSet.Setup(d => d.Provider).Returns(list.AsQueryable().Provider);
        dbSet.Setup(d => d.Expression).Returns(list.AsQueryable().Expression);
        return dbSet;
    } 

    public static IDbSet<TEntity> DbSet<TEntity>(IEnumerable<TEntity> data) 
        where TEntity : class, new()
    {
        return MockDbSet(data).Object;
    }

    public static IDbSet<TEntity> DbSet<TEntity>(params TEntity[] data)
        where TEntity : class, new()
    {
        return MockDbSet(data).Object;
    }

    // commenting out this method allowed the project to compile without the reference to EF
    public static IReturnsResult<T> ReturnsSet<T, TProperty, TEntity>(this ISetupGetter<T, TProperty> setupGetter, params TEntity[] data)
        where T : class
        where TProperty : IDbSet<TEntity>
        where TEntity : class, new()
    {
        return setupGetter.Returns((TProperty)DbSet(data));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM