Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking an insert method of a repository

I would like to unit test the following method:

void Insert(TEntity entity);

The class of this method is already mocked (I'm using Moq).

Now I'd like to do a state based test and tell Moq if this method is called, an object has to be inserted to a list. How can I do that?

useraccountRepository.Setup(r => r.Insert(useraccountBeforeLogin)).???

What comes here? There's a raises method which would raise an event. Can I use this?

like image 239
mosquito87 Avatar asked Feb 20 '13 14:02

mosquito87


1 Answers

I know this is an old thread, but this is what I did to test that items are inserted from a mocked repository, hopefully this might help someone.

var myRepositoryMock = new Mock<IMyRepository>();

var itemsInserted = new List<MyItem>();

myRepositoryMock 
    .Setup(i => i.InsertItem(It.IsAny<MyItem>()))
    .Callback((MyItem item) => itemsInserted.Add(item));
like image 176
RobJohnson Avatar answered Oct 13 '22 17:10

RobJohnson