Unit Test project insists on need to reference EntityFramework
I have an issue that just popped up in a unit test project insisting that it requires an EntityFramework reference, I am convinced that it is not needed. Other projects link to the project / extension method that the project unit test is linking / testing, and using the extension method is just fine without referencing the EntityFramework.
I found that if I just execute an extension method as a static method in a unit test project, then the unit test project compiles just fine - just completely baffled. I haven't seen anything informative about the assembly.
This won't 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());
}
Create 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 points to "var" on the line starting with "var radioButtonList", I tried to change "var" to "IHtmlString" with no change.
This compiles:
[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());
}
Create 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 ==========
RadioButtonList method signature:
public static MvcHtmlString RadioButtonList(
this HtmlHelper htmlHelper,
string name,
IEnumerable<SelectListItem> listItems,
object radioButtonHtmlAttributes = null,
object labelHtmlAttributes = null,
bool vertical = false)
source to share
I just figured out the problem. I recently added an extension method ReturnsSet
(below) to make it easier to mock IDbSet
off DbContext
in the shared unit test "helpers" project. Although I don't know why, apparently the compiler still felt like it needed a reference to the EntityFramework in order to compile the specific project that was having the problem. When I comment out a new extension method ReturnsSet
, the specific unit test project is compiled using a form of extension method syntax.
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));
}
}
source to share