简体   繁体   English

单元测试IExtension <OperationContext> 用于WCF服务

[英]Unit testing an IExtension<OperationContext> for use with WCF Service

I'm trying to develop a extension ( IExtension<OperationContext> ) for System.ServiceModel.ObjectContext using TDD. 我正在尝试使用TDD为System.ServiceModel.ObjectContext开发扩展( IExtension<OperationContext> )。 The extension is to be used as storage for a lifetime manager to be used with Windsor Castle. 该扩展名将用作与温莎城堡一起使用的终身经理的存储空间。

The problem lies in abstracting (mocking) the OperationContext. 问题在于抽象(模拟)OperationContext。 As it is a static object that gets automatically created during runtime I don't really know how to mock it (without TypeMock - which I don't have). 因为它是在运行时自动创建的静态对象,所以我真的不知道如何模拟它(没有TypeMock - 我没有)。

An OperationContext can be newed up if I supply a channel object that implements IChannelFactory, however - that interface is scary complex, and I don't know what stuff I have to implement in a stub to get it working properly. 如果我提供一个实现IChannelFactory的通道对象,可以新建一个OperationContext,但是这个界面很复杂,我不知道我必须在存根中实现什么才能让它正常工作。

Hosting the service and calling it doesn't populate the OperationContext either... 托管服务并调用它不会填充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);
    }

Any tips? 有小费吗?

This is what I ended up doing: 这就是我最终做的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM