← Older posts

Effective Unit Testing – Part 4: Entity Framework (continued)

Posted on by Ben Lucas

In my last post, I showed how you could use mocking frameworks and dependency injection to create unit tests for methods that query a database using Entity Framework. In this post, I will show how you can use the same techniques to test data access code that will insert, update or delete a record from the database.

For the purposes of demonstrating this, we will continue to use the Exchange Rate example that has been used previously and implement code so that we can insert, update, and delete Exchange Rate records.

Exchange Rate Model

To start with, we will need a model to represent the Exchange Rate.

Capture1

By creating a separate Business Model to represent the Exchange Rate, we can add additional validations or logic to the business layer without having to worry about changing the underlying Entity Model. In this case, we will have the Id on the business model represented by a nullable integer. Later, we will check whether it is null to determine whether we are inserting or updating a record.

Inserting a New Record

At this point, we are going to use some of the features of the IDbSet and will need to modify our IDatabaseEntities to support our ability to access these features. We’ll modify IDatabaseEntities as follows:

Capture2

Note that we’ve modified the ExchangeRates property to return an IDbSet rather than an IQueryable and we’ve add the SaveChanges method.

At this point, we can write the unit test for inserting a new record. We will do this by creating a mock of the IDatabaseEntities as well as a mock of the IDbSet. When inserting a record via Entity Framework, a call gets made to Add on the Entity Set and then a call is made to SaveChanges on the Data Context. The following test checks for these conditions using the Verifiable method of the mock. Whenever a Mock Setup is identified as Verifiable, this means that when the call to Verify is made at the end of the test, if the call was not made, this will result in a unit test failure.

Capture3

The call to Verify at the end of the unit test will validate that the calls to Add occurred on the IDbSet and that the call to SaveChanges occurred on the IDatabaseEntities.

Now, we will implement the method to insert the record:

Capture4

At this point, we now have a SaveExchangeRate method that will save a new record and a unit test for this functionality.

Updating an Existing Record

Next, we will write a unit test to update an existing record. In Entity Framework, to update a record, you first query for the entity from the Entity Set. After this, if you make a change to the entity and then call SaveChanges, the updates are applied to the database. The difficult part is setting up our IDbSet mock to actually allow a query to be executed against it. To do this, we can create an IQueryable out of a list by calling the AsQueryable() extension method. This would look like the following:

Capture5

Once we have the Queryable, we need to let the mock for the IDbSet know that we want it to behave as that Queryable. This is done with the following set of code:

Capture6

Now, we can write our unit test as follows:

Capture7

Next, to update the SaveExchangeRate method so that it can update an existing record, we’ll add the following else condition:

Capture8

Deleting a Record

First, we’ll write the unit test. When deleting a record from Entity Framework, the entity must first be retrieved similarly to how an entity is updated. After that, we call the Remove method on the IDbSet to remove it, and then the SaveChanges method on the Data Context. The following unit test verifies this:

Capture9

Next, the code that implements DeleteExchangeRate follows:

Capture10

Conclusion

Now we have written data access methods for inserting, updating, and deleting records from the ExchangeRate entity set. We also have unit tests that ensure these methods work as desired. If you would like to see the entire source code for my previous blog post as well as this one, the solution can be found at https://obsblogposts.codeplex.com/ in the EffectiveUnitTests-EntityFramework directory.

References

 

Remember to Review Risk

Posted on by James Adamson

The great thing about working with a passionate team of professionals is the inclination to share information, techniques, and tools – a cycle of constant improvement. I was recently in the middle of one of those discussions when it turned towards a particular application designed to capture notes. It wasn’t the first time I’d heard the praises about this particular application. I also remembered that it had finally been released on my platform of choice, Mac OS X. I popped … Continue reading

User Centered Design: Thinking Outside-In

Posted on by Kevin Sigmundson

Over the past month, I had a few opportunities to talk to our clients about customer experience (CX) and user centered design/user experience (UX) and their role in application development. At Online we see how customer experience is fundamentally changing how business operates, but the question was: do our clients see it the same way? After a month full of client meetings and giving a couple of customer experience presentations at CIPS chapter meetings, I can confidently report that the … Continue reading

← Older posts