c# - Unit test failing on EF Entry.State -


is possible unit test this?

public class myrepository<t> 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();     } } 

the [testinitialize] adds 1 user 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 test this

private myrepository<identityuser> _repository;  [testmethod] public async task updateuser_success2() {     var user = await _repository.findbyidasync("70a038cdde40");     assert.isfalse(user.emailconfirmed, "user.emailconfirmed true");      user.emailconfirmed = true;      await _repository.updateasync(user);      (...) } 

but dies on 1st line of updateasync. test wrong or updateasync implementation? there way can test it?

edit

i added suggested belogix

 dbcontext.setup(x => x.entry(it.isany<identityuser>()))                        .returns(() => dbcontext.object.entry(_user)); 

that gets me closer, think, still have non-virtual error: invalid setup on non-virtual member: x => x.entry(it.isany())

best quote ever: "all problems in computer science can solved level of indirection" - butler lampson.

it looks can't tested directly without adding additional abstraction. had refactor updateasync method way

public async task updateasync(t user) {     setentitystatemodified(user);     setpropertyismodified(user);     await _context.savechangesasync(); }  public virtual void setpropertyismodified(t user) {     _context.entry(user).property("username").ismodified = false; }  public virtual void setentitystatemodified(t user) {     _context.entry(user).state = entitystate.modified; } 

and update test code in initialize

_repository = new mock<myrepository<identityuser>>(dbcontext.object); _repository.setup(x => x.setentitystatemodified(it.isany<identityuser>())); _repository.setup(x => x.setpropertyismodified(it.isany<identityuser>())); 

my test passes

[testmethod] public async task can_update_user_details() {     //arrange     var user = await _repository.object.findbyidasync("70a038cdde40");     assert.isfalse(user.emailconfirmed, "user.emailconfirmed true");      //act                 user.emailconfirmed = true;      await _repository.object.updateasync(user);     var newuser = await _repository.object.findbyidasync("70a038cdde40");      //assert     assert.istrue(newuser.emailconfirmed, "user.emailconfirmed false"); } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -