views:

335

answers:

2

I hawe a webproject where i use StructureMap for DI IOC, it works perfect, but i dont hawe a clue how to write unit testing with StructureMap should i do this in AssemblyInitialize start Configuration of structuremap like in global.asax exept to 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...?????

Please help. Sorry for my bad english.

+5  A: 

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.

Mark Seemann
+1  A: 

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 ObjectFctory (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");
}
KevM
Would it be a bad idea to point the `ShouldEqual` comparison directly to the AppSettings value in question in this case? I won't pretend to have a clue when it comes to unit testing best practices, but as I was reading this I kept thinking it didn't seem to be confirming that it came from configuration.
patridge
Sorry, my example was very terse. I was assuming that the test setup would have given the context necessary. The setup would be responsible for making sure that application setting was present.
KevM