测试用假的DbContext和Autofixture和起订量 [英] Testing With A Fake DbContext and Autofixture and Moq
问题描述
所以按照这个例子
例如以及如何做一个假DBContex对于只使用此做工精细测试我的测试
[测试]
公共无效CiudadIndex()
{
VAR ciudades =新FakeDbSet<&城GT;
{
新的城{CiudadId = 1,EM presaId = 1,Descripcion =圣克鲁斯,FechaProceso = DateTime.Now,MarcaBaja = NULL,UsuarioId = 1},
新的城{CiudadId = 2,EM presaId = 1,Descripcion =拉巴斯,FechaProceso = DateTime.Now,MarcaBaja = NULL,UsuarioId = 1},
新的城{CiudadId = 3,EM presaId = 1,Descripcion =科恰班巴,FechaProceso = DateTime.Now,MarcaBaja = NULL,UsuarioId = 1}
}; ////创建工作单位模拟
VAR mockData =新的模拟< IContext>();
mockData.Setup(M => m.Ciudades).Returns(ciudades); //设置控制器
VAR的HomeController =新CiudadController(mockData.Object); //调用
变种的ViewResult = homeController.Index();
VAR ciudades_de_la_vista =(IEnumerable的<&城GT;)viewResult.Model; //断言..
}
现在
荫tryign使用Autofixture,起订量
打造ciudades,但我不能。我试试这个
VAR夹具=新灯();
变种ciudades = fixture.Build&所述; FakeDbSet&下;城>方式>()CreateMany&所述; FakeDbSet&下;城>>();
VAR mockData =新的模拟< IContext>();
mockData.Setup(M => m.Ciudades).Returns(ciudades);
我得到这个错误
斜面转换System.Collections.Generic.IEnumerable(FakeDbSet(城))以System.Data.Entity.IDbSet(城)
块引用>不能把<>,所以我替换为()中的错误信息
IContext和FakeDbSet实施
公共接口IContext
{
IDbSet<&城GT; Ciudades {搞定; }
}
公共类FakeDbSet< T> :IDbSet< T>其中T:类如何才能使这个工作?
解决方案一个小的点...在一样的东西:
VAR ciudades_fixture = fixture.Build<&城GT;()CreateMany<&城GT;();
第二种类型arg是不必要的,应该是:
VAR ciudades_fixture = fixture.Build<城方式>()CreateMany();
我真正明白为什么你需要一个
FakeDbSet
和文章是有点TL; DR ......总的来说,我尽量避免伪造和ORM位和碴而不是处理返回波苏斯到最大程度的可能接口。这一边......用于初始化列表中正常工作语法的原因是,有一个
添加
(和的IEnumerable $ C在DBFixture $ C>)。 AutoFixture没有直接说模式的故事(毕竟这是编译器的语法糖,而不是特别适合于反射或符合其他惯例),但你可以使用
AddManyTo
只要有一个的ICollection
中的发挥。幸运的是,FakeDbSet
的IMPL如文章中,下面让我们有在: -公开的ObservableCollection< T>本地
{
{返回_data; }
}由于
的ObservableCollection< T>
派生自的ICollection< T>
,你应该能够:VAR ciudades =新FakeDbSet< Cuidad>();
fixture.AddManyTo(ciudades.Local);VAR mockData =新的模拟< IContext>();
mockData.Setup(M => m.Ciudades).Returns(ciudades);这是可能的线了一个定制的,使这个prettier,但至少你有办法来管理它。另一种选择是有东西实现ICollection的(或添加道具的与二传手的拍摄
的IEnumerable< T>
并具有AF生成父对象,使上述集合填写。龙取代侧面说明:在您最初的问题,你有有效的:
fixture.Build< FakeDbSet<&城GT;方式>()CreateMany()
问题变得更加清晰那么 - 你问AF产生的许多的
FakeDbSet<城>
s,这是不是你想要的<。 / p>SO follow this example
example and how make a fake DBContex For test my test using just this work fine
[Test] public void CiudadIndex() { var ciudades = new FakeDbSet<Ciudad> { new Ciudad {CiudadId = 1, EmpresaId =1, Descripcion ="Santa Cruz", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1}, new Ciudad {CiudadId = 2, EmpresaId =1, Descripcion ="La Paz", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1}, new Ciudad {CiudadId = 3, EmpresaId =1, Descripcion ="Cochabamba", FechaProceso = DateTime.Now, MarcaBaja = null, UsuarioId = 1} }; //// Create mock unit of work var mockData = new Mock<IContext>(); mockData.Setup(m => m.Ciudades).Returns(ciudades); // Setup controller var homeController = new CiudadController(mockData.Object); // Invoke var viewResult = homeController.Index(); var ciudades_de_la_vista = (IEnumerable<Ciudad>)viewResult.Model; // Assert.. }
Iam tryign now to use Autofixture-Moq
to create "ciudades" but I cant. I try this
var fixture = new Fixture(); var ciudades = fixture.Build<FakeDbSet<Ciudad>>().CreateMany<FakeDbSet<Ciudad>>(); var mockData = new Mock<IContext>(); mockData.Setup(m => m.Ciudades).Returns(ciudades);
I get this error
Cant convert System.Collections.Generic.IEnumerable(FakeDbSet(Ciudad)) to System.Data.Entity.IDbSet(Ciudad)
cant put "<>" so I replace with "()" in the error message
Implementation of IContext and FakeDbSet
public interface IContext { IDbSet<Ciudad> Ciudades { get; } } public class FakeDbSet<T> : IDbSet<T> where T : class
how can make this to work?
解决方案A minor point... In stuff like:
var ciudades_fixture = fixture.Build<Ciudad>().CreateMany<Ciudad>();
The second type arg is unnecessary and should be:
var ciudades_fixture = fixture.Build<Ciudad>().CreateMany();
I really understand why you need a
FakeDbSet
and the article is a bit TL;DR... In general, I try to avoid faking and mucking with ORM bits and instead dealing with interfaces returning POCOs to the max degree possible.That aside... The reason the normal syntax for initialising the list works is that there is an
Add
(andIEnumerable
) in DBFixture. AutoFixture doesn't have a story for that pattern directly (after all it is compiler syntactic sugar and not particularly amenable to reflection or in line with any other conventions) but you can useAddManyTo
as long as there is anICollection
in play. Luckily, within the impl ofFakeDbSet
as in the article, the following gives us an in:-public ObservableCollection<T> Local { get { return _data; } }
As
ObservableCollection<T>
derives fromICollection<T>
, you should be able to:var ciudades = new FakeDbSet<Cuidad>(); fixture.AddManyTo(ciudades.Local); var mockData = new Mock<IContext>(); mockData.Setup(m => m.Ciudades).Returns(ciudades);
It's possible to wire up a customization to make this prettier, but at least you have a way to manage it. The other option is to have something implement ICollection (or add a prop with a setter taking
IEnumerable<T>
and have AF generate the parent object, causing said collection to be filled in.
Long superseded side note: In your initial question, you effectively have:
fixture.Build<FakeDbSet<Ciudad>>().CreateMany()
The problem becomes clearer then - you are asking AF to generate Many
FakeDbSet<Ciudad>
s, which is not what you want.这篇关于测试用假的DbContext和Autofixture和起订量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!