Showing posts with label Testing. Show all posts
Showing posts with label Testing. Show all posts

Friday, September 8, 2017

How to Mock Entity Framework when using Repository pattern and async methods

What we are testing

Imagine you are using the repository pattern or something similar and it is implemented something like this. I like this as the DbContext (InvoiceModel is a subclass of it) is not held for just the duration of this method. This is a very simple example with no parameters or corresponding Where() being used.

public class InvoiceRepository
    {
        public async Task<IEnumerable<Invoice>> GetInvoicesAync()
        {
            using (var ctx = new InvoiceModel())
            {
                return await ctx.Invoices.ToListAsync();
            }
        }
    }

The first challenge and solution

The problem with this that it is not really possible to mock out the Entity Framework because we can't get a reference to the DbContext (InvoiceModel) since it is created here. That is no problem, we can use the Factory pattern to allow us to have the advantage of holding it just for the duration of this method, but also allowing us to mock it. Here is how we might amend the above InvoiceRepository class to use the InvoiceModelFactory might be used.


public class InvoiceRepository
    {
        private IInvoiceModelFactory _invoiceModelFactory;

        public InvoiceRepository(IInvoiceModelFactory invoiceModelFactory)
        {
            _invoiceModelFactory = invoiceModelFactory;
        }


        public async Task<IEnumerable<Invoice>> GetInvoiceAsync()
        {
            using (var ctx = _invoiceModelFactory.Create())
            {
                return await ctx.Invoices.ToListAsync();
            }
        }
    }

What's the factory pattern?

The factory pattern is useful delaying the creation of an object (our InvoiceModel in this case). If we also create an interface for the factory (IInvoiceModelFactory) and also gives us the ability to change the factory in testing to create whatever kind of implementation of IInvoiceModel that we want to.

public interface IInvoiceModelFactory
    {
        InvoiceModel Create();
    }

public class InvoiceModelFactory : IInvoiceModelFactory
    {
        public InvoiceModel Create()
        {
            return new InvoiceModel();
        }
    }

Mock the Entity Framework 

When I say mock the Entity Framework, it really ends up being DbContext which is InvoiceModel in our case. I'm using NSubstitute, but any mocking framework should be able to be used. To help with mocking the entity framework I recommend using EntityFramework.NSubstitute. There are version of it for most mocking frameworks. It provides the implementation of SetupData() below.

NB. If you are using a newer version of NSubstitute than EntityFramework.NSubstitute requires you can get the source and build it yourself. It is really only 6 files.

Helpers methods for improved readability of tests

There are some methods I created to wire up the required mocks and make the tests easier to read.

private IInvoiceModelFactory GetSubstituteInvoiceModelFactory(List<Invoice> data)
{
var context = GetSubstituteContext(data);
var factory = Substitute.For<IInvoiceModelFactory>();
factory.Create().Returns(context);
return factory;
}

private InvoiceModel GetSubstituteContext(List<Invoice> data)
{
var set = GetSubstituteDbSet<Invoice>().SetupData(data);
var context = Substitute.For<InvoiceModel>();
context.Invoices.Returns(set);
return context;
}

private DbSet<TEntity> GetSubstituteDbSet<TEntity>() where TEntity : class
{
return Substitute.For<DbSet<TEntity>, IQueryable<TEntity>, IDbAsyncEnumerable<TEntity>>();
}

Writing the Test

Now it is pretty straight forward to write the actual test. We create the invoice data that would be in the database as a List of Invoices. 

[TestMethod]
public async Task GetInvoicesAsync_ReturnsInvoices()
{
//Arrange
var invoices = new List<Invoice>
{
new Invoice(),
new Invoice()
};

var factory = GetSubstituteInvoiceModelFactory(invoices);
        var invoiceRepo = new InvoiceRepository(factory);

//Act
var result = await invoiceRepo.GetInvoicesAsync();

//Assert
Assert.IsTrue(result.Any());
}

That is it. :)

Friday, September 1, 2017

Testing Private Methods

There are times where it is much easier to test a private method instead of the public method. I could agrue this is a not a good idea, but let's assume this is what we want to do.


Option 1: Reflection

You can access anything via Reflection, but it can be a bit difficult to read, particularly for a test. This is probably the hardest to figure out and I would not recommend it.

Option 2: PrivateObject

You can use PrivateObject to invoke the private method in your unit test. 

The syntax would be something like this:

var calculator = new Calculator();
var privateLogic = new PrivateObject(calculator);
privateLogic.Invoke("Add", 1, 1);

To make this easier and more user friendly you could wrap this logic up into a method. A step further you could create an extension method for Calculator in your test project that has an extension method called Add with the same parameters as the private Add method. Then from the test perspective it would act like the private method is public.


Option 3: ReflectionMagic

Another more intuitive option is ReflectionMagic available on Nuget that uses dynamic objects to expose private bits. It can be used as a syntactically easy way to access most anything you can using Reflection. The upside of this is that you don't need to do anything special to access the private method. Unfortunately there is no compiler or Intellisense to help you with the parameters to pass it, but no special coding is needed.

You could use it something like this:

var calculator = new Calculator();
calculator.AsDynamic().Add(1,1);

This is so easy, and requires no special code it probably is not worth writing the wrapper method as I talked about the the PrivateObject option.

NOTE: I had mixed results with ReflectionMagic, but the same raw source code did work.

Friday, July 21, 2017

Unit Testing internal methods in c#



Sometimes you want to unit test private or protected methods on a class in one of your assemblies (MyAssembly). The problem is the unit test assembly (MyAssembly.Tests) is just another assembly and has to respect the access modifiers such as private, protected, public, internal, etc.

I generally subscribe to the notion that if you have a private method there is a good chance you should be moving that functionality to another class and using dependency injection to make that logic public and testable.

There are however exceptions to this such as when overriding a protected or protected method for example. While the same principle could be applied, it may or may not be the best choice. For cases where you deem it to not the best choice there is a solution.

In your assembly (of the class you are testing) you can add the following to the AssemblyInfo.cs file to grant special rights to any class (our unit test assembly in this case) such that it has access to internal (sorry private, protected, etc still can't) methods and thus allowing you to test them.

[assembly: InternalsVisibleTo("MyAssembly.Tests")]

If you really need to test private methods you can use PrivateObject to invoke the private method in your unit test. This feels a bit dirty though.

Friday, May 19, 2017

Code Contracts

Ever want an common way to do a null parameter check or that an integer is positive, etc. If so, you may find MS Code Contracts useful. The downside is that all your files then have a dependency on this assembly. The upside is they are available in the System.Diagnostics.Contracts namespace which is part of the mscorlib.dll assembly so it should always be available.

Code Contracts provide a way to specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits. Object invariants describe the expected state for a class that is in a good state.

The key benefits of code contracts include the following:
  • Improved testing: Code contracts provide static contract verification, runtime checking, and documentation generation.
  • Automatic testing tools: You can use code contracts to generate more meaningful unit tests by filtering out meaningless test arguments that do not satisfy preconditions.
  • Static verification: The static checker can decide whether there are any contract violations without running the program. It checks for implicit contracts, such as null dereferences and array bounds, and explicit contracts.

Monday, March 13, 2017

Test Resources

Testing Strategies in a Microservice Architecture - outstanding and applicable to monolithic applications also

Wednesday, July 20, 2016

Customizing Code Coverage in VS2015

The code coverage in Visual Studio 2015 by default includes the test code itself. This is often not desired. Below are some links go pages to help with this.

Customizing Code Coverage Analysis
Using Code Coverage to Determine How Much Code is being Tested
Troubleshooting Code Coverage
Troubleshooting missing data in Code Coverage Results

My conclusion is that the default settings that comes Visual Studio 2015 is not sufficient because it includes the test code in the test results. I found the .runsettings file to be a necessary change. When I did this, I was tempted to exclude test assemblies to the list of modules to exclude, but found this actually stopped the tests from being reported on. Instead I found it better to use namespace exclusions using the function tags.

For example,

   <Functions>
              <Exclude>
                <!--Exclude (Tests from the results) any functions in namespaces that have Test in them-->
                <Function>.*Test.*</Function>

I also found it useful to exclude tests (classes or methods) from the code coverage results that use particular attributes on them. For example,

 <Attributes>
              <Exclude>
                <!--Don't forget "Attribute" at the end of the name -->
                
                                <Attribute>^Microsoft\.VisualStudio\.TestTools\.UnitTesting\.TestClassAttribute$</Attribute>
                <Attribute>^TechTalk\.SpecFlow\.GivenAttribute$</Attribute>
                <Attribute>^TechTalk\.SpecFlow\.WhenAttribute$</Attribute>
                <Attribute>^TechTalk\.SpecFlow\.ThenAttribute$</Attribute>


I did however add any assemblies that have their own unit tests and code coverage reports to the list of modules to exclude. That way the code coverage of these assemblies is not counted twice.

The rest of the .runsettings file can be just as the sample file from MS.
Also, Here is a reporting tool that helps show code coverage results in a more user friendly manner.

Monday, June 20, 2016

Links for Powershell

Microsoft Team Foundation Server Client - Nuget package to integrate with TFS (version control, work item tracking, build, etc via REST APIs

Get Started with the REST APIs - shows the url format, usage, etc for TFS REST APIs.

TFS API Part 33 - Get Build Definitions and Build Details - example of how to get Build definition details.

Creating a Build Definition using the TFS 2013 API - actually in C#, but should work for Powershell also.

Pester - PowerShell testing. Support in VS2015 now.

Thursday, November 26, 2015

Good Articles on MVC and .NET

Getting Started

Implementing Basic CRUD Functionality with the Entity Framework in ASP.NET MVC Application - Good step by step tutorial for MVC 5 (not MVC 6). It also shows how to use TryUpdateModel to specify what can be bound to each object. This is a bit cleaner and more object specific, but does require a bit of code.

Binding 

ASP.NET MVC - The Features and Foibles of ASP.NET MVC Model Binding - a great post that gives an in-depth explanation of  how the binder works and how it can be extended.

Prefixing Input Elements Of Partial Views With ASP.NET MVC - Explains how to make a partial view generate html with references that the Binder can understand properly. Also shows a generic method for passing the prefix to the Partial View. It doesn't say it, but creating Edit Templates and EditorFor() instead of Partial Views will also solve this problem.

Model Binding To A List - Explains how to set the Name html form property so that the binder will create the collection.

Mass Assignment / Over-posting

6 Ways To Avoid Mass Assignment in ASP.NET MVC - If you use the Include or Exclude parameters with the Bind attribute it doesn't seem so say it anywhere, but the names of the fields are the same as what show up in Request.Form. So, things like Person.Address.Name, Person.Address.ID, and Person.Address would all need to be added to the Include parameter in order for fields bound to related objects to be allowed through the Include() list and be added to the Request.Form colletion.

Sharing Create / Edit Screens

 ASP.NET MVC - using the same form to both create and edit - forum on how this could be implemented

View Model

How to Use ViewModel with ASP.NET MVC - shows how to implement the repository pattern, how to organize your project, and how to use a View Model.

Videos

Building Applications in ASP.NET MVC 4  - very in depth video on how to build MVC applications. Most of it still applies to MVC 6. It has details on certain topics that are not covered in the MVC 5 version of the video.

Building Applications in ASP.NET MVC 5 - very good video and in depth video on how to build MVC applications.

Best Practices

Best Practices for ASP.NET MVC  - this is a bit old, from 2010, but still has some good advise.
 

Dependency Injection

Dependency Injection and Unit Of Work using Castle Windsor and NHibernate - good example of how to use DI and UOW in MVC application. It shows NHibernate, but it can be used for Entity Framework. It also shows how to use in the context of the different layers of an application.

Castle Windsor Tutorial 1 - I highly recommend reviewing this tutorial. It shows how to create a Castle Windsor container application. It is complex enough to see how a whole application can be done with only calling the container 3 times. It also found it useful to modify the code such that it does NOT use IoC (i.e. not using Castle Windsor). This involves instantiating objects by hand. Then a line at a time, I removed the code I added to hardcode the creation of an object and added the appropriate line in the Installer for that object. Run the application between changes to see how the container actually instantiates the objects automatically once they are registered (in the installer).

Castle Windsor Tutorial 2 - In the case where you do need to create your own instances of an object and still use IoC, you should use TypedFactoryFacility.

Krzysztof Koźmic on software - talks about IoC concepts in depth.

What's New

Top 10 Changes in ASP.NET 5 and MVC 6

Entity Framework

Configuring Relationships with the Fluent API i.e. configuring Cascade delete and one-to-one relationships.

Unit Testing

Testing Entity Framework with a MOQ - step by step instructions on how to test the EF6+ using MOQ. I am using the latest EF6 and did NOT have to change the class the inherits from DbContext such that the DbSets are virtual because they are already that way in the T4 templates.Also, if you need to access the .Set method of the DbContext then you will need to tell the mock what it should be returning using something like: mockContext.Setup(m => m.Set()).Returns(mockSet.Object); See here for more details.

Attributes for MSTesting - includes samples of attributes for setup and cleanup methods that apply to tests, classes, assemblies, etc depending on the scope you need. A class can be created that has assembly specific setup and cleanup and doesn't need to have any tests actually in the class itself. Teh class does need to be marked as TestClass() though.

Unit Testing Good Patterns #3 - Know Your Moq Argument Matchers - this is an excellent read to understand how to use the It and Verify classes.

Keeping up

Code Magazine

Entity Framework Team Blog

Monday, August 1, 2011

Screen-scraping / Automation Tools

In general I don’t recommend screenscraping at all, and it should only be a last resort. I do recommend test automation of UI if you are developing software though. Screenscraping will consume a ton of time to build it, and then to maintain it when the screens change in very minor ways. The main reason is inevitably the thing you want access to on the screen is not easy to get to and interact with. In many cases a tool will get you 80% to 90% of the way there and then you will hit a block wall or at least an endless pit that sucks all your time and resources to find a solution.

Assuming you have decided that screen-scraping is worth it or you are doing UI test automation I do have a few recommendations if you use C# and are on the Windows platform. Some things to consider: What are you trying to scrap? What type of content are you trying to scrape? For example, is it a web page (does it have AJAX), Silverlight, or is it a desktop app (Is it Java or Native Windows). Will this be on a server and if so can you install FireFox (FF) or Internet Explorer (IE)? How will you can these tools. It also depends on how you will execute tool. For example, some tools can be called directly from C# while other are Java or even XML based. Some can be called from command line other can’t.

Before I recommend the tool I HIGHLY recommend commenting the heck out of your code with Page and Page element you are working with. It is easy enough to write it and figure out what page you are and what element on that page you are working with when you are writing it for the first time because you are looking at it. However, when something changes and you have to modify it, it will be very time consuming to try to debug and/or figure out what you were doing. The reason is that often code becomes very cryptic because you are navigating through arrays of arrays or arrays or in general trying to get to things that can difficult to do.

Ok, enough lecturing, here is what I recommend:

 

Tool

HTML (No AJAX)

HTML (w/ AJAX)

Silverlight

Windows Desktop

Java Application

Language

Comment

White

N

N

Y

Y

Y

C#

This is my tool of choice for Silverlight or Windows Desktop or even Java applications. It reminds me of Watin (see below) as far as programming style and being easy and intuitive. Requires IE/FF. Don’t use the mouse when running, but can “help script” if gets stuck by moving mouse.

Watin

Y

Y

N

N

N

C#

In general this is the tool of choice for HTML with or without AJAX. Easy and intuitive to work with.

WebHarvest

Y

N

N

N

N

Java or Command Line

This is nice because it simulates browser so no browser needed, but this also means no support for AJAX or javascript. It is XML based and you don’t really write java code, you write XML, so it is all declarative programming. Kind of different, but quick and effective for plain HTML sites.

Selenium Y Y * See Silverlight-Selenium N N C#/Java, Command Line, and most popular languages. I would try Watin first in most cases, but don’t discount Selenium either. It is a scalable solution that is supported by many languages and platforms and browsers. Has a recorder that can be useful. Has complete IDE. XPath based which I find not as intuitive or easy to debug as Watin, but can be effective at navigating a page.
Silverlight-Selenium N N Y N N C# This still uses Selenium is actually just an extension to Selenium. I think White is easier to use and a bit more robust, but if White doesn’t work for your Silverlight app try this.

Monday, May 17, 2010

Unit Testing Asynchronous calls in Visual Studio 2010

I am simply amazed how much effort I had to go through to figure out how to test asynchronous calls in Visual Studio 2010 (VS2010). In the end, I was able to figure it out with the help of some blogs that I read.

VS2010 ships with integrated Unit Testing which I would like to take advantage of. I am writing a Silverlight application that calls a Windows Workflow Foundation service that I implemented using the WCF Workflow Service Application. I really like it, but I want to be able to unit test the workflow service.

The only way I found to test a WCF Workflow Service Application that I could find was to add a Service Reference to my Unit Test project. This is good for me because that is how Silverlight will call it. The problem is that the WCF Workflow Service Application can’t be called synchronously. So, we have to call it Asynchronously. The problem is that the Unit Test framework used in VS2010 does not support Asynchronous calls in  a Unit Test. Well, it runs, but doesn’t wait for the response to the Async call, so the test is pretty worthless.

Now that you know what I am trying to do, here is what I found as solutions.

Option 1: Simulate Synchronous call using an Asynchronous call

Here is a class I created to simplify the process of make an asynchronous call appear to be synchronous.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyApp.Tests
{
public class AsyncTest
{

// max number of milliseconds to wait for an asynchronous call
int timeout = -1;

public AsyncTest()
{
// if debugging, make it a much larger like infinity
if (System.Diagnostics.Debugger.IsAttached)
{
timeout = -1; // infinity (wait for ever)
}
else
{
timeout = 20 * 1000; // 10 seconds
}
}

public AsyncTest(int timeout)
{
this.timeout = timeout;
}

// we'll use this to make the current thread wait until our asynchrous call finishes
ManualResetEvent block = new ManualResetEvent(false);

// we'll use this to flag if/when our async call finishes
bool isAsyncDone = false;

public void Done()
{
isAsyncDone = true; // flag that we are done (do NOT do this after calling block.Set() this will cause race conditions!!!!)
block.Set(); // tell the calling / this thread that it can continue now.
}

public void Wait()
{
block.WaitOne(timeout, false); // wait until block.Set() is called or the timeout expires (which ever comes).
Assert.IsTrue(isAsyncDone, "Test took too long"); // if it took too long then report it to the test framework.
block.Reset(); // set the event to non-signaled before making the next asynchronous call.
}
}
}

Here is an example of how you would use it to create a method that acts like a synchronous method, but calls an asynchronous WCF Service.

public GetWorkflowStatusCompletedEventArgs GetWorkflowStatus(long requestID)
{
AsyncTest at = new AsyncTest();

GetWorkflowStatusCompletedEventArgs returnedArgs = null;

// setup our service reference and callback for when it is done
ServiceClient wf = new ServiceClient();
wf.GetWorkflowStatusCompleted += delegate(object sender, GetWorkflowStatusCompletedEventArgs e)
{
returnedArgs = e;
at.Done();
};


wf.GetWorkflowStatusAsync(requestID);
at.Wait();

return returnedArgs;
}

I created one of these methods for each of the asynchronous methods I wanted to test. In fact I created a helper class to hold them. Now, in my class that has all my tests in it, I just call the methods on this helper class which are synchronous. Now the test run properly.

For completeness, here is what the unit test (testmethod) would look like.

[TestMethod]
public void TestCanGetWorkflowStatusTwiceInARow()
{
var status = Helper.GetWorkflowStatus(1234);
Assert.AreEqual<long>(status.RequestID, requestData.RequestID, "The wrong request id was returned.");
Assert.IsTrue(status.RequestID > 0);
}
Now I can write a unit test just as easily as I do any other unit test. The synchronous / asynchronous issue is encapsulated in a helper class. I like it. Not much extra work either. Especially since each helper method I write is almost identical. It could be generated if desired (using CodeSmith, etc).
I wish I could take credit for all this, but I can’t. The solution / implementation is completely mine, but the underlying technique is borrowed. For more info on those links, see here:

Option 2: Use the Silverlight Unit Test Application

I think this method is a reasonable approach, but for testing a WCF Service, it seems a bit unnatural to me. I like Option 1 better because I want my test results to be managed in VS2010. If nothing else other than no browser opens and also that you can block check-in of code if tests fail. The integrated Unit testing just seems a bit more integrated with VS2010.
I do think the Silverlight Unit Test Application is a great testing technology. However, I think it is best and most natural for testing Silverlight applications, not the web services they call.
There are lots of good blogs on the subject, so I won’t repeat it here. Here are some of the blogs that I found particularly useful when I went down this road.

Friday, July 25, 2008

A brief overview of Testing

Why do we need to test anyway?
  • Living documentation of the expected behavior of the system
  • To build confidence that software works and still works after making a change
  • Reduce cost by finding bugs early
  • A method to document requirements and verify solution meets the requirements
  • Quality assurance including behavior, performance, etc
Questions to think about?
  • When you make a change, how do you currently determine if the change does what it is supposed to?
  • ... that it didn't break something else?
  • ... where do you document that this is a new requirement?
  • ... how much time will the next programmer have to spend to be able to verify that your requirement still works after his or her new change?
Types of Functional Testing
  • Unit Testing - tests of methods or classes, but not across them.
  • Regression Testing - typically uses unit tests to verify functionality still works as expected
  • System Testing - testing across classes and methods. This is general higher level and tests more of how the system behaves vs the requrements rather than implementation.
  • Integration Testing - tests across classes and methods; it stri
  • Acceptance Testing - done by the end user to give their stamp of approval
Types of Non-Functional Testing Black Box Testing Testing that assumes you don't know the implementation of what is in the box. In this type of testing you have inputs and outputs, but you don't know how inputs are mapped to outputs. In this scenario you think of the box as something you bought or got from someone else, and don't really know how it works, just what it is supposed to do. Examples are: Use case testing, White Box Testing Testing that assumes you DO know the implementation of what is in the box. In fact, the purpose of the test is to make sure that all paths of execution in the box are tested (or at least the ones that can break if you subscribe to Extreme Programming techniques). Typically boundaries, ranges of values, control flow, data flow, and other code execution testing is the focus. Examples are: Unit Testing Test Driven Development Is an approach to design better software. Test Driven Development (TDD) starts the development cycle with gathering requirements, but then quickly moving to writing test cases that document the requirement and force the designer to think about exactly what the system is supposed to do. It takes automated unit testing and regression testing and makes them a basis for all development activities. This means that when adding new functionality a test is written first, it will fail this test at first, then as it is implemented it will eventually pass the test. The best part is now refactoring can take place because we have regression testing already in place to verify by refactoring that we did not break anything. This promotes clean, clear, modularized code because if you can test the code it is likely that it is modular and easier to maintain.