gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:45:21 35 4
gpt4 key购买 nike

我刚在单元测试项目中遇到一个问题,坚持认为它需要 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)

最佳答案

我刚刚弄清楚了这个问题。我最近添加了一个扩展方法 ReturnsSet(如下),以便在共享单元测试“帮助程序”中更轻松地模拟 IDbSets 与 DbContexts项目。虽然我不知道为什么,显然编译器仍然觉得它需要引用 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));
}
}

关于c# - 单元测试项目坚持要求引用 EntityFramework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27707796/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com