Moq - Update with DbEntityEntry

.net c# entity-framework-6 moq

Question

I employ EF6. The produced code looks like this:

public partial class MyDataContext : DbContext
{
    public MyDataContext() : base("name=mydata")
    {
    }

    public virtual DbSet<Book> Books { get; set; }
}

I also have a general repository, such as:

public class GenericRepository<TObject> where TObject : class
{
    protected readonly MyDataContext Context;

    protected GenericRepository(MyDataContext context)
    {
        Context = context;
    }

    public virtual TObject Update(TObject data, int id)
    {
        if (data == null)
            return null;

        TObject obj = Context.Set<TObject>().Find(id);
        if (obj != null)
        {
            Context.Entry(obj).CurrentValues.SetValues(data);
            Context.SaveChanges();
        }

        return obj;
    }
}

Then I have a service that updates data using the GenericRepository:

public class MyDataService<TObject> where TObject : class
{
    private readonly MyDataContext context;

    public MyDataService(MyDataContext ct)
    {
        context = ct;
    }

    public TObject Update(TObject obj, int id)
    {
        var r = new GenericRepository<TObject>(context);
        return r.Update(obj, id);
    }
}

I need to update a book so I can say something like this:

var ds = new MyDataService<Book>(new MyDataContext());
var data = ds.Update(new Book { Name = "New Name" }, 1);

Everything is going well. I then attempt to utilise Moq to unit test the code mentioned above using examples like:

var updatedBook = new Book { Name = "Update Book Name" };

var mockSet = new Mock<DbSet<Book>>();
var mockContext = new Mock<MyDataContext>();
mockContext.Setup(c => c.Books).Returns(mockSet.Object);
mockContext.Setup(c => c.Set<Book>().Find(It.IsAny<object[]>()))
            .Returns<object[]>(ids => chips.FirstOrDefault(d => d.Id == (int)ids[0]));

var service = new MyDataService<Book>(mockContext.Object);
var data = service.Update(updatedBook, 1);

However, I get an exception on theContext.Entry(obj).CurrentValues.SetValues(data) line.

How can I correctly spoof the Update method?

1
1
12/5/2016 8:59:00 PM

Accepted Answer

ZZZ_tmp
2
12/5/2016 9:51:10 PM

Popular Answer

You may create an interface forMyDataService so that you may parody it

public Interface IMyDataService<TObject> where TObject : class
{
   TObject Update(TObject obj, int id);
}

public class MyDataService<TObject>:IMyDataService<TObject>
 where TObject : class
{
    private readonly MyDataContext context;

    public MyDataService(MyDataContext ct)
    {
        context = ct;
    }

    public TObject Update(TObject obj, int id)
    {
        var r = new GenericRepository<TObject>(context);
        return r.Update(obj, id);
    }
}

Moq:

var mockDataService = new  Mock<IMyDataService<Book>>();
mockDataService.Setup(c=> c.Update(It.Any<Book>(),It.Any<int>()).Returns(updatedbook);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow