How to expect a call on a function, if the parameter value is dynamically changing?

14 views
Skip to first unread message

Raju

unread,
Dec 19, 2008, 7:03:12 AM12/19/08
to Rhino.Mocks
Hi All,

I have a record and playback portion.

Let us say,

Declaration:
int myInt = 0;

Record()
mockInterface.fun1(myInt);

Playback()
//Asks user to enter some value for myInt.
// Suppose the user entered 10 as input
//now call the function
mockInterface.fun1(myInt);

Rhino Mock is failing in this case:
Expected value of myInt is '0', actual value is '10'

Please suggest us to validate the variable myInt.

Thanks in advance.

Regards,
Raju.

Ayende Rahien

unread,
Dec 19, 2008, 7:39:48 AM12/19/08
to Rhino...@googlegroups.com
Take a look at constraints

Raju

unread,
Dec 19, 2008, 7:54:52 AM12/19/08
to Rhino.Mocks
Hi Ayende,

Thanks for a quick reply. I had this variable in a class.
So I am using Property.ValueConstraint.
But the problem is same.

Please give me suggestion on this.

Thanks in advance.

Regards,
Raju.

> > Raju.- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

unread,
Dec 19, 2008, 7:57:07 AM12/19/08
to Rhino...@googlegroups.com
Don't use Value contraint use the Is.Matching( delegate )

Raju

unread,
Dec 19, 2008, 9:57:53 AM12/19/08
to Rhino.Mocks
Hi Ayende,

Very thankful to you.

Is.Matching(delegate) is giving me problem that my test class is not
marked as serializable.
Can you sugget me another method for me?
Which will work for dynamic variable checking?

Regards,
Naga Raju.
> > > - Show quoted text -- Hide quoted text -

Ayende Rahien

unread,
Dec 19, 2008, 10:01:44 AM12/19/08
to Rhino...@googlegroups.com
I have never seen something like that before, please show the code

Raju

unread,
Dec 20, 2008, 5:00:04 AM12/20/08
to Rhino.Mocks
Hi Ayende,

This is the error I am getting while executing my test script:
Test method TestProject1.UnitTest1.T1 threw exception:
System.Runtime.Serialization.SerializationException: Type
'TestProject1.UnitTest1+<>c__DisplayClass4' in Assembly 'TestProject1,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked
as serializable..

Record()
Predicate<object> myConstraint = (delegate(object msg)
{
RequestResponseRequest dynData = msg as
RequestResponseRequest;
if (dynData != null){
return ((dynData.Header.resultCode ==
reqRequest.Header.resultCode));}
else { return false; } });

RequestResponseResponse respRequest = new
RequestResponseResponse(respMine);
mockService.RequestResponse(reqRequest);
simControl.LastCall<RequestResponseResponse>
().Return(respRequest).Constraints(Is.Matching<object>(myConstraint));
simControl.MockRepositoryControl.ReplayAll();

Playback()
req.resultCode = 10;
var resp = realService.RequestResponse
(reqRequest); simControl.MockRepositoryControl.VerifyAll
();

If I debug it, it is going into Matching(), then after coming out of
it, it is throwing exception.

I am attaching folder into the "files section" of the group. Please
suggest me what I need to change.

File name: To_RhinoMockGrp.zip.

Regards,
Raju.

Ayende Rahien

unread,
Dec 20, 2008, 7:01:08 PM12/20/08
to Rhino...@googlegroups.com
You are using remoting to move the object between one app domain and the next.

You can solve the issue by defining an explicit class and make it serializable, then passing a delegate to a method on that,

Raju

unread,
Dec 20, 2008, 11:40:35 PM12/20/08
to Rhino.Mocks
Hi Ayende,

Very very thankful to you.

I am poor in the remoting. Can you tell me, do I need to create a
class in test project or in my main project?
I was little bit puzzled with the present scenario. Which method do I
need to call? Because I am getting the error in LastCall<>() method,
so far I understand that.

I will be very thankful to you once again.

Regards,
Naga Raju.

Ayende Rahien

unread,
Dec 21, 2008, 12:00:15 AM12/21/08
to Rhino...@googlegroups.com
The Rhino Mocks repository is running in a separate appdomain.
You are creating an anonymous delegate, Make this an explicit method on a class that has Serializable on it and then create a new instance of it and send the delegate to the method

Raju

unread,
Dec 22, 2008, 4:38:50 AM12/22/08
to Rhino.Mocks
Hi Ayende,

I have created a class like below:
[System.SerializableAttribute()]
public class MyClass
{
public delegate bool MyOwnDelegate(object msg);
public bool MyFunction(object msg)
{
RequestResponseRequest msgToSend = msg as
RequestResponseRequest;
if (msgToSend != null)
{

//return ((msgToSend.Header.resultCode ==
reqRequest.Header.resultCode));
return true;
}
else
{
return false;
}
}
}

and created an object in unit test:
MyClass mineClass = new MyClass();

When I called it using a delegate,
Predicate<bool> myConstraint = (delegate { return myDelHere
(reqRequest); });

It is still throwing me the same error. If delete "delegate" in the
above statement, it is giving unable to Convert bool to Prediate<bool>
type. If work on it also, again the same error.

Here in the above statement I used another delegate. I tried with
function also. but the problem is same.

Please suggest me, what shall I do?

Regards,
Raju.

On Dec 21, 12:00 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> The Rhino Mocks repository is running in a separate appdomain.You are

Ayende Rahien

unread,
Dec 22, 2008, 7:09:29 AM12/22/08
to Rhino...@googlegroups.com
Create _small_ test case showing the problem.

Raju

unread,
Dec 22, 2008, 7:58:44 AM12/22/08
to Rhino.Mocks
Hi Ayende,

1) Serializable class which will have delegate and a method
[System.SerializableAttribute()]
public class MyClass
{
public delegate bool MyOwnDelegate(object msg);
public bool MyFunction(object msg)
{
RequestResponseRequest msgToSend = msg as
RequestResponseRequest;
if (msgToSend != null)
{

//return ((msgToSend.Header.resultCode ==
reqRequest.Header.resultCode));
return true;
}
else
{
return false;
}
}
}

Test Method containing Record() and Playback():
[TestMethod]
public void T1()
{
MyClass mineClass = new MyClass();

IHeaderType req = new IHeaderType()
{
resultCode = 0,
CodeReturn = 0,
};

RequestResponseRequest reqRequest = new
RequestResponseRequest(req);
try
{
//using(simControl.MockRepositoryControl.Record())
{
//using (simControl.MockRepositoryControl.Ordered
())
{

#if(MY_STUB)
{
IHeaderType respMine = new IHeaderType()
{
resultCode = 1,
CodeReturn = 1,
};

//Predicate<object> myConstraint =
MyOwnMethod();

MyClass.MyOwnDelegate myDelHere = new
MyClass.MyOwnDelegate(mineClass.MyFunction);


Predicate<bool> myConstraint = (delegate
{ return myDelHere(reqRequest); });

RequestResponseResponse respRequest = new
RequestResponseResponse(respMine);

mockService.RequestResponse(reqRequest);

simControl.LastCall<RequestResponseResponse>().Return
(respRequest).Constraints(Is.Matching<bool>(myConstraint));
simControl.MockRepositoryControl.ReplayAll
();
}
#endif
}// End of Ordered
}// End of Record

//using (simControl.MockRepositoryControl.Playback())
{
req.resultCode = 10;
var resp = realService.RequestResponse
(reqRequest);
Assert.IsTrue(resp.IHeader.resultCode == 1);
simControl.MockRepositoryControl.VerifyAll();
autoEvent.Set();

}
}
catch (ExpectationViolationException ex)
{
Assert.Fail(ex.Message);

}
}

Here myDelHere is the delegate from the outer class which is
serialzed.
Predicate<bool> myConstraint = (delegate { return myDelHere
(reqRequest); });
In the above statement if I dont use delegate key word, it is giving
me error that unable to convert bool to Predicate<object> type.

Regards,
Raju.

Ayende Rahien

unread,
Dec 22, 2008, 7:59:59 AM12/22/08
to Rhino...@googlegroups.com
Please attach the code, I can't extract it from the email

Raju

unread,
Dec 22, 2008, 8:20:14 AM12/22/08
to Rhino.Mocks
Hi Ayende,

I have attached them in the files section. you can find it with
today's date.

Regards,
Raju.
> ...
>
> read more »- Hide quoted text -

Ayende Rahien

unread,
Dec 22, 2008, 8:27:11 AM12/22/08
to Rhino...@googlegroups.com
that is NOT small

Raju

unread,
Dec 22, 2008, 8:33:56 AM12/22/08
to Rhino.Mocks
Hi Ayende,

Ya... But I got a problem in uploading files. There was a problem with
uploading.
So I uploaded every thing using other members system.

Extremely sorry for the trouble being given.

Regards,
Raju.

Ayende Rahien

unread,
Dec 22, 2008, 8:36:51 AM12/22/08
to Rhino...@googlegroups.com
Sorry, I am not able to dechiper what you are doing there.
Please create _small_, _isolated_ test case, which I'll look at.

Raju

unread,
Dec 22, 2008, 8:58:29 AM12/22/08
to Rhino.Mocks
Hi Ayende,

My Serializable Class:
[System.SerializableAttribute()]
public class MyOwnClass
{
public delegate bool MyOwnDelegate(object msg, int result);
public bool MyFunction(object msg, int result)
{
RequestResponseRequest msgToSend = msg as
RequestResponseRequest;
if (msgToSend != null)
{
return ((msgToSend.Header.resultCode == result));
}
else
{
return false;
}
}
}

My test method:
[TestMethod]
public void TestMethod1()
{
MyOwnClass mineClass = new MyOwnClass();

IHeaderType req = new IHeaderType()
{
resultCode = 0,
CodeReturn = 0,
};
RequestResponseRequest reqRequest = new
RequestResponseRequest(req);

#if(MY_STUB)
{
IHeaderType respMine = new IHeaderType()
{
resultCode = 1,
CodeReturn = 1,
};

MyOwnClass.MyOwnDelegate myDelHere = new
MyOwnClass.MyOwnDelegate(mineClass.MyFunction);

//Predicate<bool> myConstraint = (delegate { return myDelHere
(reqRequest, req.resultCode); });
Predicate<bool> myConstraint = (delegate { return
mineClass.MyFunction(reqRequest,req.resultCode); });

RequestResponseResponse respRequest = new
RequestResponseResponse(respMine);

mockService.RequestResponse(reqRequest);

simControl.LastCall<RequestResponseResponse>().Return
(respRequest).Constraints(Is.Matching<bool>(myConstraint));

simControl.MockRepositoryControl.ReplayAll();
}
#endif
req.resultCode = 10;
var resp = realService.RequestResponse(reqRequest);
Assert.IsTrue(resp.IHeader.resultCode == 1);
simControl.MockRepositoryControl.VerifyAll
();

}

Once again I am sorry, I could not abe to upload a simple file. Every
file .cs or .txt, any file, it is giving security warning.

Here my aim is, I initialized one variable with 0. In Record mode, I
am calling a delegate. In playback mode i am changing the variable
value from 0 to 10. So my intention is that, with the delegate and
is.matching(), the result code should be updated to 10 and it should
pass.

This scenario is working for me in a normal project. But with
remoting, I am getting this serializable problem.

Regards,
Raju.

Regards,
Raju.

Ayende Rahien

unread,
Dec 22, 2008, 9:25:39 AM12/22/08
to Rhino...@googlegroups.com
Attach a file to this email

Raju

unread,
Dec 22, 2008, 12:59:00 PM12/22/08
to Rhino.Mocks
Hi Ayende,

I have kept the file in files section of the group. I was unable to
send as an attachment to this mail.

The file name is : UnitTest1.cs

Regards,
Raju.

Ayende Rahien

unread,
Dec 22, 2008, 7:33:07 PM12/22/08
to Rhino...@googlegroups.com
The problem is here:
MyClass.MyOwnDelegate myDelHere = new MyClass.MyOwnDelegate(mineClass.MyFunction);

Predicate<bool> myConstraint = (delegate { return myDelHere(reqRequest); });

You should have something like:

Predicate<bool> myConstraint = (mineClass.MyFunction);


Reply all
Reply to author
Forward
0 new messages