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

时间:2014-12-30 15:06:27

标签: c# unit-testing visual-studio-2013 msbuild

我有一个问题刚出现在一个单元测试项目中,坚持要求引用EntityFramework,我确信它不需要它。其他项目正在引用单元测试项目引用/测试的项目/扩展方法,并且使用扩展方法就好了而没有引用EntityFramework。

我发现如果我只是在单元测试项目中将扩展方法作为静态方法执行,那么单元测试项目编译得很好 - 只是完全困惑。我没有在构建输出中看到任何信息。

这不编译:

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

构建输出:

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 ==========

错误指向以“var radioButtonList”开头的行中的“var”,我尝试将“var”更改为“IHtmlString”而没有任何更改。

这会编译:

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

构建输出:

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 ==========

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

1 个答案:

答案 0 :(得分:1)

我刚刚发现了这个问题。我最近添加了一个扩展方法ReturnsSet(下面),以便在共享单元测试“帮助程序”项目中更轻松地模拟IDbSet s DbContext s。虽然我不知道为什么,显然编译器仍然认为它需要引用EntityFramework来编译出现问题的特定项目。当我注释掉新的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));
    }
}