Problem with abstract/virtual methods and wcf.

96 views
Skip to first unread message

Edward

unread,
Sep 7, 2010, 6:16:09 AM9/7/10
to Rhino.Mocks
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();
}
}
}

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

Tim Barcz

unread,
Sep 8, 2010, 3:36:04 PM9/8/10
to rhino...@googlegroups.com
What error do you get, when I copy/paste the code above I get (is this what you're seeing):

Test 'Rhino.Mocks.Tests.FieldsProblem.Tester.Test' failed: System.ServiceModel.AddressAccessDeniedException : HTTP could not register URL http://+:9090/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
  ----> System.Net.HttpListenerException : Access is denied
TearDown : System.ServiceModel.CommunicationObjectFaultedException : The communication object, System.ServiceModel.ServiceHost, cannot be used for communication because it is in the Faulted state.
at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
FieldsProblem\FieldProblem_ Edward.cs(85,0): at Rhino.Mocks.Tests.FieldsProblem.Tester.Setup()
--HttpListenerException
at System.Net.HttpListener.AddAll()
at System.Net.HttpListener.Start()
at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
--TearDown
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Close()
FieldsProblem\FieldProblem_ Edward.cs(91,0): at Rhino.Mocks.Tests.FieldsProblem.Tester.TearDown()




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




--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Tim Barcz

unread,
Sep 8, 2010, 3:45:59 PM9/8/10
to rhino...@googlegroups.com
Can you explain the purpose of the test?  I see that in the test setup (Setup()) you're calling .Open() after setting an endpoint to http://localhost:9090

This seems odd to me, but I don't know exactly what you're trying to test in order to be able to give useful feedback.

If you give me more info I can dig in.

Tim

Edward

unread,
Sep 9, 2010, 11:01:11 AM9/9/10
to Rhino.Mocks
The error you get means that another proccess is running on 9090 port
or that you do not have administrator rights.

The purpose is to test a component that uses WCF services. The
component is an abstract class so i want to mock the abstract methods
as well as the WCF services that it uses.

On Sep 8, 10:45 pm, Tim Barcz <timba...@gmail.com> wrote:
> Can you explain the purpose of the test?  I see that in the test setup
> (Setup()) you're calling .Open() after setting an endpoint tohttp://localhost:9090
>
> This seems odd to me, but I don't know exactly what you're trying to test in
> order to be able to give useful feedback.
>
> If you give me more info I can dig in.
>
> Tim
>
>
>
>
>
> On Wed, Sep 8, 2010 at 2:36 PM, Tim Barcz <timba...@gmail.com> wrote:
> > What error do you get, when I copy/paste the code above I get (is this what
> > you're seeing):
>
> > Test 'Rhino.Mocks.Tests.FieldsProblem.Tester.Test' failed:
> > System.ServiceModel.AddressAccessDeniedException : HTTP could not register
> > URL http://+:9090/. Your process does not have access rights to this
> > namespace (seehttp://go.microsoft.com/fwlink/?LinkId=70353for details).
> >   ----> System.Net.HttpListenerException : Access is denied
> > TearDown : System.ServiceModel.CommunicationObjectFaultedException : The
> > communication object, System.ServiceModel.ServiceHost, cannot be used for
> > communication because it is in the Faulted state.
> >  at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
> > at
> > System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener
> > channelListener)
> >  at
> > System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransport­ManagersCallback
> > selectTransportManagerCallback)
> > at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan
> > timeout)
> >  at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan
> > timeout)
> > at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
> >  at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan
> > timeout)
> > at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
> >  at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
> > at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
> >  at System.ServiceModel.Channels.CommunicationObject.Open()
> > FieldsProblem\FieldProblem_ Edward.cs(85,0): at
> > Rhino.Mocks.Tests.FieldsProblem.Tester.Setup()
> >  --HttpListenerException
> > at System.Net.HttpListener.AddAll()
> > at System.Net.HttpListener.Start()
> >  at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
> > --TearDown
> >  at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan
> > timeout)
> > at System.ServiceModel.Channels.CommunicationObject.Close()
> >  FieldsProblem\FieldProblem_ Edward.cs(91,0): at
> > Rhino.Mocks.Tests.FieldsProblem.Tester.TearDown()
>
> > On Tue, Sep 7, 2010 at 5:16 AM, Edward <grigoria...@vsoft.gr> wrote:
>
> >> 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
> >> {
>
> >>  //-------------------------------------------------------------------------­------
> >>  //-------------------------------------------------------------------------­----------------------------
>
> >>    [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(Serv­iceContractAttribute));
>
> >> Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(typeof(Oper­ationContractAttribute));
> >>  //-------------------------------------------------------------------------­--------------------------
> >> }
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Rhino.Mocks" group.
> >> To post to this group, send email to rhino...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> rhinomocks+...@googlegroups.com<rhinomocks%2Bunsubscribe@googlegrou­ps.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/rhinomocks?hl=en.
>
> > --
> > Tim Barcz
> > Microsoft C# MVP
> > Microsoft ASPInsider
> >http://timbarcz.devlicio.us
> >http://www.twitter.com/timbarcz
>
> --
> Tim Barcz
> Microsoft C# MVP
> Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz- Hide quoted text -
>
> - Show quoted text -

Tim Barcz

unread,
Sep 9, 2010, 11:05:46 AM9/9/10
to rhino...@googlegroups.com, Rhino.Mocks
I don't see that in the tests...I will gladly write the test for you if you give me a scenario...

> To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.

Edward

unread,
Sep 10, 2010, 4:35:28 AM9/10/10
to Rhino.Mocks
The code i posted is an example that replicates the problem.
The test scenario is the following. I have the AbstractShape class
which has an abstract method CalculateArea.
The Circle class (which is also abstract) has a CalculateArea
implementation and also an abstract GetRadius method.
The CalculateArea of the Circle uses a WCF service
(ICalculatorService) which contains a Multiply method.
I want to test the Circle.CalculateArea method, so i'm replacing the
ICalculatorService with a mock WCF service.
The problem is that when i set an expectation on the Circle.GetRadius
method and the ICalculatorService.Multiply method
i get a Timeout exception when the Multiply method is called from the
Circle.CalculateArea method.


On Sep 9, 6:05 pm, Tim Barcz <timba...@gmail.com> wrote:
> I don't see that in the tests...I will gladly write the test for you if you give me a scenario...
>
> >> Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz-Hide quoted text -
>
> >> - Show quoted text -
>
> > --
> > You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
> > To post to this group, send email to rhino...@googlegroups.com.
> > To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/rhinomocks?hl=en.- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages