Is this possible for unit test?
public class MyRepository<T> where T : IdentityUser, new() { public async Task UpdateAsync(T user) { _context.Entry(user).State = EntityState.Modified; _context.Entry(user).Property("UserName").IsModified = false; await _context.SaveChangesAsync(); } }
[TestInitialize] adds 1 user to the repository
_user = new IdentityUser { Id = "70a038cdde40" }; IDbSet<IdentityUser> users = new FakeDbSet<IdentityUser> { _user }; var dbContext = new Mock<MyDbContext<IdentityUser>>(); dbContext.Setup(x => x.Users).Returns(() => users); _repository = new MyRepository<IdentityUser>(dbContext.Object);
and I'm trying to check with this
private MyRepository<IdentityUser> _repository; [TestMethod] public async Task UpdateUser_Success2() { var user = await _repository.FindByIdAsync("70a038cdde40"); Assert.IsFalse(user.EmailConfirmed, "User.EmailConfirmed is True"); user.EmailConfirmed = true; await _repository.UpdateAsync(user); (...) }
But he dies on the first line of UpdateAsync. Is the test incorrect or an implementation of UpdateAsync? Is there any way to check this?
Edit
I added as Belogix suggested
dbContext.Setup(x => x.Entry(It.IsAny<IdentityUser>())) .Returns(() => dbContext.Object.Entry(_user));
This is closer to me, I think, but still have a non-virtual error: incorrect setting for a non-virtual element: x => x.Entry (It.IsAny ())
c # unit-testing entity-framework
kooshka
source share