Hi, i have the following problem: i want to test an abstract class
that contains a method that calls a wcf service. I mock the wcf
service and use a partial mock to mock the abstract class. The problem
is that the wcf service throws a timeout exception when i set an
expectation to the abstract class.The example below replicates the
issue.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using System.ServiceModel;
using Rhino.Mocks.Interfaces;
using System.ServiceModel.Channels;

namespace TestLib
{
    
//-------------------------------------------------------------------------------

    [ServiceContract]
    public interface ICalculatorService
    {
        [OperationContract]
        [FaultContract(typeof(ArgumentNullException))]
        double Multiply(double x, double y);
    }

    public abstract class AbstractShape
    {
        protected internal abstract double CalculateArea();

        public double GetArea()
        {
            //Do some other stuff...
            return CalculateArea();
        }
    }

    public abstract class Circle : AbstractShape
    {
        //This method should be abstract, i made it virtual just to
see
        //that when not setting an expectation the test will pass.
        protected internal virtual double GetRadius()
        {
            return 15;
        }

        protected internal override double CalculateArea()
        {
            ICalculatorService client =
ChannelFactory<ICalculatorService>
                .CreateChannel(new BasicHttpBinding(), new
EndpointAddress("http://localhost:9090";));

            double radius = GetRadius();
            double result = client.Multiply(radius, radius);

            result = client.Multiply(Math.PI, result);

            ((IChannel)client).Close();

            return result;
        }
    }

    
//-----------------------------------------------------------------------------------------------------

    [TestFixture]
    public class Tester
    {
        private MockRepository _mocks;
        private ICalculatorService _calculatorServiceMock;
        private ServiceHost _calculatorServiceHost;
        private Circle _circleMock;

        [SetUp]
        public void Setup()
        {
 
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(typeof(ServiceContractAttribute));
 
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(typeof(OperationContractAttribute));

            _mocks = new MockRepository();
            _calculatorServiceMock =
_mocks.DynamicMock<ICalculatorService>();
            _circleMock = _mocks.PartialMock<Circle>();

            _calculatorServiceHost = new
ServiceHost(_calculatorServiceMock, new Uri[] { new Uri("http://
localhost:9090") });
 
_calculatorServiceHost.AddServiceEndpoint(typeof(ICalculatorService),
new BasicHttpBinding(), String.Empty);
 
_calculatorServiceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode
= InstanceContextMode.Single;
            _calculatorServiceHost.Open();
        }

        [TearDown]
        public void TearDown()
        {
            _calculatorServiceHost.Close();
        }

        [Test]
 
[ExpectedException(typeof(FaultException<ArgumentNullException>))]
        public void Test()
        {
            using (_mocks.Record())
            {
                //If we don't set this expectation the test will pass.
                Expect.Call(_circleMock.GetRadius()).Return(17);

                Expect.Call(_calculatorServiceMock.Multiply(0,
0)).IgnoreArguments()
                    .Throw(new
FaultException<ArgumentNullException>(new
ArgumentNullException("Forced Exception!")));
            }

            _mocks.ReplayAll();

            try
            {
                _circleMock.GetArea();
            }
            finally
            {
                _mocks.VerifyAll();
            }
        }
    }

    
//---------------------------------------------------------------------------------------------------
}

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to