【发布时间】:2014-10-29 10:53:00
【问题描述】:
使用 MEF (System.ComponentModel.Composition) 可以将模拟对象添加到容器中。
container.ComposeExportedValue(mock.Object);
参考:How to use Moq to satisfy a MEF import dependency for unit testing?
如何使用便携式 MEF 库 (System.Composition) 实现这一点?
关于更多上下文,我将发布我目前掌握的一些代码。
我正在内存中的 ASP.NET Web API 上创建 xBehave.net 集成测试。
我是这样设置客户端的。
config = new HttpConfiguration();
WebApiConfig.Register(config);
config.DependencyResolver = MefConfig();
server = new HttpServer(config);
Client = new HttpClient(server);
Request = new HttpRequestMessage();
我将我的 MEF 配置设置为 WebApiContrib.IoC.Mef 的默认配置。
private static IDependencyResolver MefConfig()
{
var conventions = new ConventionBuilder();
conventions.ForTypesDerivedFrom<IHttpController>().Export();
conventions.ForTypesMatching(
t => t.Namespace != null && t.Namespace.EndsWith(".Parts"))
.Export()
.ExportInterfaces();
var container = new ContainerConfiguration()
.WithAssemblies(
new[] { Assembly.GetAssembly(typeof(ICache)) }, conventions)
.CreateContainer();
return new MefDependencyResolver(container);
}
这是我要测试的控制器的签名。它从缓存中读取。
public MyController(ICache cache) { }
这是测试。模拟是使用Moq 创建的。
[Scenario]
public void RetrieveOnPollingRequest()
{
const string Tag = "\"tag\"";
string serverETag = ETag.Create(Tag);
"Given an If-None-Match header"
.f(() => Request.Headers.IfNoneMatch.Add(
new EntityTagHeaderValue(Tag)));
"And the job has not yet completed"
.f(() =>
{
string tag = serverETag;
this.MockCache.Setup(x => x.StringGet(tag)).Returns(Tag);
});
"When retrieving jobs"
.f(() =>
{
Request.RequestUri = uri;
Response = Client.SendAsync(Request).Result;
});
"Then the status is Not-Modified"
.f(() =>
Response.StatusCode.ShouldEqual(HttpStatusCode.NotModified));
}
那么我如何将那个模拟放入容器而不是已经导出的部分?还是我不?我需要去使用不同的 IoC 容器吗?
【问题讨论】:
标签: c# asp.net-web-api mef bdd ioc-container