Group Testing LINQ to SQL CRUD Operations Using Moq

I looked around for other questions, but nothing really matches what I'm looking for ... mainly because I'm not 100% sure what I'm looking for!

I mainly work on a new project at the moment, I created my abstraction level for DB entities and set the DAC as repositories. I want to use unit testing for this with Mock objects, however I hit a smart wall using CRUD operations (specifically C), my unit test class:

[TestClass] public class RepositoryTest { private Mock<IPersonRepository> _repository; private IList<IPerson> _personStore; [TestInitialize()] public void Initialize() { _personStore= new List<IPerson>() { new Person() { Name = "Paul" }, new Person() { Name = "John" }, new Person() { Name = "Bob" }, new Person() { Name = "Bill" }, }; _repository = new Mock<IPersonRepository>(); _repository.Setup(r => r.Entirely()).Returns(_personStore.AsQueryable()); } [TestCleanup()] public void Cleanup() { _personStore.Clear(); } [TestMethod()] public void Can_Query_Repository() { IEnumerable<IPerson> people = _repository.Object.Entirely(); Assert.IsTrue(people.Count() == 4); Assert.IsTrue(people.ElementAt(0).Name == "Paul"); Assert.IsTrue(people.ElementAt(1).Name == "John"); Assert.IsTrue(people.ElementAt(2).Name == "Bob"); Assert.IsTrue(people.ElementAt(3).Name == "Bill"); } [TestMethod()] public void Can_Add_Person() { IPerson newPerson = new Person() { Name = "Steve" }; _repository.Setup(r => r.Create(newPerson)); // all this Create method does in the repository is InsertOnSubmit(IPerson) // then SubmitChanges on the data context _repository.Object.Create(newPerson); IEnumerable<IPerson> people = _repository.Object.Entirely(); Assert.IsTrue(people.Count() == 5); } } 

My Can_Query_Repository method is successful, however, the Can_Add_Person method does not give confirmation. Now I need to do:

  • Set the .Create method of the Mock repository to add an item to _personStore?
  • Do something like that?
  • It is impossible to give up hope, what I want to achieve, and I am doing it all wrong!

As always, any help / advice is appreciated!

+4
source share
1 answer

Ideally, you would do some integration tests for them, but if you want a unit test, then there are possible ways, including those that were not mentioned in the comments to the original question.

First. When testing your crud, you can use .Verify to check if Create methods are actually called.

 mock.Verify(foo => foo.Execute("ping")); 

With Verify, you can verify that the argument was a specific argument, of a specific type and the number of times the method was actually called.

Second. Or, if you want to check the actual objects that have been added to the repository collection, you can use the .Callback method for your layout.

In your test, create a list that will receive the objects you create. Then, on the same line where you call your customization, add a callback that will insert the created objects into the list.

In your statements, you can check the items in the list, including their properties, to ensure that the correct items have been added.

 var personsThatWereCreated = new List<Person>(); _repository.Setup(r => r.Create(newPerson)).Callback((Person p) => personsThatWereCreated.Add(p)); // test code // ... // Asserts Assert.AreEuqal(1, personsThatWereCreated.Count()); Assert.AreEqual("Bob", personsThatWereCreated.First().FirstName); 

Provided in your actual example, you create the person and then add him to the setting. Using the callback method here will not be useful.

You can also use this technique to increment a variable to count the number of times it was called.

+6
source

All Articles