Moq - Hollow Method

I'm trying to understand how this Mock works (using the Moq framework), but I'm a little confused about the methods returning void.

The source object has the following methods / properties:

void Add(Person entity);
void Save();
IQueryable<Person> Persons;

The Add method calls InsertOnSubmit(this is Linq for sql), and the Save method calls Context.SubmitChanges(). The Person property returns an object Table<News>.

I am not sure how I am deceiving these methods, since I obviously cannot use it Returns().

Or does my design mean that I can't really mimic objects correctly?

+5
source share
1 answer

It depends on what you are going to test.

Moq , .

, , , -, , , . , , Add , Save Persons , . :

var mockedClass = new Mock<IDaoClass>();
var classUnderTest = ...

mockedClass.SetupGet(m => m.Persons).Returns(new List<Person>().AsQueryable());

classUnderTest.DoSomething();

mockedClass.Verify( m => m.Add(It.IsAny<Person>())); 
mockedClass.Verify( m => m.Save());

- , Moq , lambdas, .

+3

All Articles