我正在尝试使用 TDD 为 System.ServiceModel.ObjectContext 开发一个扩展(IExtension<OperationContext>
)。该扩展将用作与温莎城堡一起使用的生命周期管理器的存储。
问题在于抽象(模拟)OperationContext。因为它是一个在运行时自动创建的静态对象,所以我真的不知道如何模拟它(没有 TypeMock - 我没有)。
如果我提供实现 IChannelFactory 的 channel 对象,则可以更新 OperationContext,但是 - 该接口(interface)非常复杂,而且我不知道我必须在 stub 中实现什么才能使其正常工作。
托管服务并调用它也不会填充 OperationContext...
[TestFixtureSetUp]
public void FixtureSetup()
{
_serviceHost = new TypeResolverServiceHost(typeof(AilDataService));
_serviceHost.AddServiceEndpoint(typeof (IAilDataService), new BasicHttpBinding(), SvcUrl);
_serviceHost.Open();
var endpointAddress = new EndpointAddress(SvcUrl);
_ailDataService = ChannelFactory<IAilDataService>.CreateChannel(new BasicHttpBinding(), endpointAddress);
}
[TestFixtureTearDown]
public void FixtureCleanup()
{
_serviceHost.Close();
}
[Test]
public void Can_Call_Service()
{
var reply = _ailDataService.GetMovexProductData("169010", new TaskSettings{MovexDatabase = "MVXCDTATST", MovexServer = "SEJULA03"});
Assert.That(reply, Is.Not.Null);
// This fails
Assert.That(OperationContext.Current!=null);
}
有什么建议吗?
最佳答案
这就是我最终做的:
[TestFixture]
public class WcfPerSessionLifestyleManagerTests
{
private const string SvcUrl = "http://localhost:8732/Design_Time_Addresses/JulaAil.DataService.WcfService/AilDataService/";
private TypeResolverServiceHost _serviceHost;
private ChannelFactory<IAilDataService> _factory;
private IAilDataService _channel;
private WindsorContainer _container;
[Test]
public void Can_Populate_OperationContext_Using_OperationContextScope()
{
using (new OperationContextScope((IContextChannel) _channel))
{
Assert.That(OperationContext.Current, Is.Not.Null);
}
}
}
关于c# - 单元测试 IExtension<OperationContext> 以用于 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2855616/