Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using StructureMap with unit tests

I'm using StructureMap in a web project for DI IOC. It works perfect, but I don't have a clue how to write unit tests with StructureMap.

Should I do this in AssemblyInitialize start Configuration of StructureMap like in global.asax except for datacontext not to use live LinqToSqlDataContext but some memory data like this:

 [AssemblyInitialize]
 public static void Start()
 {
        ObjectFactory.Configure(x =>
                {
                    x.For<IDataContext>().HttpContextScoped().Use<MemoryDataContext>()
                        .Ctor<string>("connectionString")
                        .Is(ConfigurationManager.ConnectionStrings["DEVConnection"].ConnectionString);
                    x.For<IDepartamentRepository>().Use<DepartamentDB>();
                    x.For<IDevelopmentProcess>().Use<DevelopmentProcesses>().OnCreation(c => c.User = Current.CurrentUser);
                    x.For<IActivityProcess>().Use<ActivitiesProcess>().OnCreation(c=> c.User = Current.CurrentUser);
                    x.For<IDevDeveloperRepository>().Use<DevDeveloperDB>();
                    x.For<IDevelopmentRepository>().Use<DevelopmentDB>();
                    x.For<IActivityRepository>().Use<ActivityDB>();
                    x.For<IActivityTypeRepository>().Use<ActivityTypeDB>();
                    x.For<IDevUserRepository>().Use<DevUsersDB>();
                    x.For<IAttachmentRepository>().Use<AttachmentDB>();
                }
            );
 }

and then use ObjectFactory.GetInstance() testing or how do I do this?

like image 902
Florim Maxhuni Avatar asked Feb 07 '10 10:02

Florim Maxhuni


People also ask

Should you separate unit and integration tests?

Keep your testing suites separateIntegration tests should not be run together with unit tests. Developers working on specific business logic in the code must be able to run unit tests and get near-immediate feedback to ensure that they haven't broken anything before committing code.

Should I write unit tests for models?

Thereby, models should be thoroughly tested. Besides, data is the most important component in any web project. Thereby, ensuring models have sufficient validations, and they don't allow junk data into db is extremely crucial.


2 Answers

I agree with Mark. Testability is one of the primary reasons you are likely using a container in the first place.

There are times where creating an integration test for your container setup might be a good idea. For example if you have any behavior in your container configuration you will want to create tests for that behavior. In you container config you set IDataContext's connection string via the configuration manager.

The following code is similar to what I do to test such a setup. Notice I avoid ObjectFactory (static singleton objects have their own problems) and wrap my container setup in a bootstrapper helper class:

[Test]
public void connection_string_should_come_from_application_configuration()
{
  var container = new ContainerBootstraper().Container;

  var connectionString = container.GetInstance<IDataContext>().ConnectionString

  connectionString.ShouldEqual("test project application configuration connection string");
}
like image 161
KevM Avatar answered Sep 24 '22 20:09

KevM


You shouldn't need to use a DI Container in unit tests at all.

A container is something you use to wire components together, but a unit test is a test of each component in isolation.

like image 32
Mark Seemann Avatar answered Sep 21 '22 20:09

Mark Seemann