Testing with fake DbContext and Autofixture and Moq

SO follow this example

example and how to make a fake DBContex For a test test, using only 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.. } 

Now I will try to use Autofixture-Moq

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 converts System.Collections.Generic.IEnumerable (FakeDbSet (Ciudad)) to System.Data.Entity.IDbSet (Ciudad)

cant put "<>", so I replace with "()" in the error message

Implement IContext and FakeDbSet

 public interface IContext { IDbSet<Ciudad> Ciudades { get; } } public class FakeDbSet<T> : IDbSet<T> where T : class 

how to do it?

+4
source share
3 answers

A small point ... In materials like:

 var ciudades_fixture = fixture.Build<Ciudad>().CreateMany<Ciudad>(); 

The second type arg is not needed and should be:

 var ciudades_fixture = fixture.Build<Ciudad>().CreateMany(); 

I really understand why you need FakeDbSet , and the article is a bit TL, DR ... In general, I try to avoid falsification and dropping of ORM bits and instead refers to interfaces that return POCOs to the maximum extent.

Aside ... The reason that the usual syntax for initializing a list works is because DBFixture has Add (and IEnumerable ). AutoFixture does not have a history for this template directly (after all, it is the syntax of the compiler and is not particularly reflective or conforms to any other conventions), but you can use AddManyTo as long as there is ICollection to play. Fortunately, in the FakeDbSet test, as in the article, the following gives us: -

 public ObservableCollection<T> Local { get { return _data; } } 

Since ObservableCollection<T> comes from ICollection<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); 

You can connect the setting to make it more beautiful, but at least you have a way to control it. Another option is to implement ICollection something (or add support from a setter that accepts IEnumerable<T> , and create an AF parent object, causing this collection to fill up.


Long side note: in your original question, you actually have:

 fixture.Build<FakeDbSet<Ciudad>>().CreateMany() 

Now the problem is getting clearer: you're asking for autofocus to generate a lot of FakeDbSet<Ciudad> s, which you don't want.

+3
source

I have not used AutoFixture after a while, but should not:

 var ciudades = new FakeDbSet<Ciudad>(); fixture.AddManyTo(ciudades); 
+1
source

At the moment, when I finish this, I will continue to read about how to use automoq, because I'm new to this

 var fixture = new Fixture(); var ciudades_fixture = fixture.Build<Ciudad>().CreateMany<Ciudad>(); var ciudades = new FakeDbSet<Ciudad>(); foreach (var item in ciudades_fixture) { ciudades.Add(item); } var mockData = new Mock<IContext>(); fixture.Create<Mock<IContext>>(); mockData.Setup(r => r.Ciudades).Returns(ciudades); 
0
source

Source: https://habr.com/ru/post/955817/


All Articles