Mocking concrete classes Rhino Mocks 2.5.9

332 views
Skip to first unread message

fokosh

unread,
Jan 24, 2006, 10:51:47 AM1/24/06
to Rhino.Mocks
I'm new to Rhino Mocks and the realm of Unit Testing and Mocking.

I'm mocking a concrete class.... the CreateMock method passed, but when
i call
Expect.Call(any method or any property of my mock object).Return(any
object);

the framework throws System.InvalidOperationException: Invalid call,
the last call has been used or no call has been made.

what really annoys me is that the sample of mocking the ArrayList in
the documentation works fine, i even created an empty class with one
method
func() which simply returns an integer and it still throws the same
exception

Please help me is there anything i did wrong....!!!!!

Here is my code

public class MyClass
{
public int func()
{
return 90;
}

public MyClass()
{

}
}

[Test]
public void testMyClass()
{
MyClass MyClass1;
MockRepository mocks = new MockRepository();
MyClass1 = (MyClass)mocks.CreateMock(typeof(MyClass));
Expect.Call(MyClass1.func()).Return(994);
mocks.ReplayAll();
Assert.AreEqual(999, MyClass1.func());
mocks.VerifyAll();
}

teom

unread,
Jan 24, 2006, 12:18:21 PM1/24/06
to Rhino.Mocks
On a concrete class, you can only mock "virtual" methods.

public class MyClass
{
virtual public int func()
{
return 90;
}
public MyClass()
{
}
}


Teo

Message has been deleted

fokosh

unread,
Jan 25, 2006, 3:05:06 AM1/25/06
to Rhino.Mocks
Thanx Teo for replying but can you tell me please why it restricted to
virtual methods only, is it only in this framework (because of its
design), so i want to test my class methods and properties.. i should
make all of it virtuals !!!???. thanks

David Chelimsky

unread,
Jan 25, 2006, 8:03:08 AM1/25/06
to Rhino...@googlegroups.com
This is a restriction imposed by the C# language. If you were writing
mocks by hand, you might subclass the class you wish to mock, and
override the methods you wish to mock. Can't do that unless the method
is virtual (or abstract).

On 1/25/06, fokosh <fok...@yahoo.com> wrote:
>
> thanx Teo for replying but can you tell me please why it restricted to
> virtual methods only, is it only in this framework (because of the
> design i mean). so i want to test my class methods and properties i

Ayende Rahien

unread,
Jan 25, 2006, 3:46:25 PM1/25/06
to Rhino...@googlegroups.com
The issue here is that you can't really mock something that you said you don't want to change (by making it non virtual).
The framework will not let you do it. There are ways around it using the profiling API, but they have their own problems and they don't allow the natural syntax that Rhino Mocks has (instead they make you use strings, yuck!).

fokosh

unread,
Jan 29, 2006, 8:57:53 AM1/29/06
to Rhino.Mocks
can somen help plz and tell me how to use mocking with out and ref
parameters plz
how do i test out and ref parameters thanx ??!!!

Ayende Rahien

unread,
Jan 29, 2006, 2:27:28 PM1/29/06
to Rhino...@googlegroups.com
Rhino supports Ref & Out, if you pass a delegate that will set those values.
What help do you need?

 
Message has been deleted

fokosh

unread,
Jan 30, 2006, 7:19:50 AM1/30/06
to Rhino.Mocks
/// <summary>
/// Get data from the database
/// </summary>
/// <param name = "queryInfo">QueryInfo object to track query
execution</param>
/// <returns></returns>

public virtual DataTable GetData(out string queryState, out string
queryException)
{
DataTable outTable;
outTable = new DataTable();
try
{
this.OpenConnection();
this.ExecuteReader();

outTable = this.GetTableFromReader();

queryState = "Finished";
queryException = "None";

sqlReader.Close();
this.CloseConnection();
}
catch(System.Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
queryState = "Finished";
queryException = "None";
}
finally
{
this.CloseConnection();
}

return outTable;
}

Ayende Rahien

unread,
Jan 31, 2006, 2:53:14 AM1/31/06
to Rhino...@googlegroups.com
Foo foo = (Foo)mocks.CreateMock(typeof(Foo));
string queryState, queryException;
Expect.Call(foo.GetData(out queryState, out queryException)).Return(new DataTable()
  .Call(MehtodThatWillSetQueryStateAndException)


 
On 1/30/06, fokosh <fok...@yahoo.com> wrote:

can you show me a sample plz here is my code:

fokosh

unread,
Feb 1, 2006, 4:42:30 AM2/1/06
to Rhino.Mocks
Hi Ayende Rahein
i tried your code but there is no Expect.Call(foo.GetData(out
queryState, out queryException)).Return(new
DataTable()
---> .Call(MehtodThatWillSetQueryStateAndException) there is only
Callback method which takes a delegate....

I used the Callback method but it didnt work... it kept throwing this
exception: TestCase 'FactoriesUnitTests.FactoriesManagerTest.GetData'
failed: System.InvalidOperationException: This action is invalid when
the mock object is in record state.

Here is my test method

[Test]
public void GetData()
{
GetDataDelegate getDataDelegate = new
GetDataDelegate(SetQueryStateAndException);

Expect.Call(Helper.RunInstanceMethod(typeof(DatabaseManager),
"OpenConnection",
databaseManagerMock, null)).Return(true);

Expect.Call(Helper.RunInstanceMethod(typeof(DatabaseManager),
"ExecuteReader",
databaseManagerMock, null)).Return(true);

// The exception is thrown in the next method call
Expect.Call(databaseManagerMock.GetData(out queryState, out
queryException)).Return(outTable).Callback(getDataDelegate);

Expect.Call(Helper.RunInstanceMethod(typeof(DatabaseManager),
"CloseConnection",
databaseManagerMock, null)).Return(true);

mocks.ReplayAll();

//......Assert calls here
}

fokosh

unread,
Feb 1, 2006, 4:43:08 AM2/1/06
to Rhino.Mocks
Hi Ayende Rahein
i tried your code but there is no Expect.Call(foo.GetData(out
queryState, out queryException)).Return(new
DataTable()

Ayende Rahien

unread,
Feb 1, 2006, 12:40:16 PM2/1/06
to Rhino...@googlegroups.com
Sorry about that, it's the Do() method, IIRC.


-----Original Message-----
From: Rhino...@googlegroups.com [mailto:Rhino...@googlegroups.com] On Behalf Of fokosh
Sent: Wednesday, February 01, 2006 11:43 AM
To: Rhino.Mocks
Subject: Re: Mocking concrete classes Rhino Mocks 2.5.9

Reply all
Reply to author
Forward
0 new messages