单元测试IExtension <operationcontext>以与WCF服务</operationcontext>一起使用

时间:2010-05-18 08:29:33

标签: c# wcf unit-testing

我正在尝试使用TDD为System.ServiceModel.ObjectContext开发扩展(IExtension<OperationContext>)。该扩展名将用作与温莎城堡一起使用的终身经理的存储空间。

问题在于抽象(模拟)OperationContext。因为它是在运行时自动创建的静态对象,所以我真的不知道如何模拟它(没有TypeMock - 我没有)。

如果我提供一个实现IChannelFactory的通道对象,可以新建一个OperationContext,但是这个界面很复杂,我不知道我必须在存根中实现什么才能使它正常工作。

托管服务并调用它不会填充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);
    }

任何提示?

1 个答案:

答案 0 :(得分:0)

这就是我最终做的事情:

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