使用StructureMap单元测试 [英] Using StructureMap with unit tests

查看:212
本文介绍了使用StructureMap单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是在国际奥委会DI Web项目StructureMap。它完美的作品,但我没有线索如何编写单元测试与StructureMap。

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.

我应该这样做的AssemblyInitialize在Global.asax中开始StructureMap的配置一样,除了对的DataContext不使用活LinqToSqlDataContext但一些像这样的存储器中的数据:

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>();
                }
            );
 }

,然后使用ObjectFactory.GetInstance()测试或我该怎么做呢?

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

推荐答案

您应该不需要的在单元测试中使用DI容器的。

一个容器是用来电线组件在一起的东西,但一个单元测试是单独的每个组件的测试。

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

这篇关于使用StructureMap单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆