Why this simple test IS NOT FAILING?!

5 views
Skip to first unread message

Tarpan

unread,
Oct 30, 2009, 9:54:41 AM10/30/09
to Rhino.Mocks
Guys, Here is a simple test:
TestClass calls the TestMock twice and there is a
Assert...Repeat.Once. The test runs just fine.
I know I'm doing something wrong. But what?

-------------
Imports MbUnit.Framework
Imports Rhino.Mocks

<TestFixture()> _
Public Class AAA_test
<Test()> _
Public Sub Test1()
Dim TestMock As ITestMock = MockRepository.GenerateStub(Of
ITestMock)()
Dim TestClass As CTestClass = New CTestClass
TestClass.test(TestMock)
TestMock.AssertWasCalled(Function(x) x.TestFunc, Function
(options) options.Repeat.Once)
End Sub
End Class


Public Class CTestClass
Public Sub test(ByVal TestMock As ITestMock)
TestMock.TestFunc()
TestMock.TestFunc()
End Sub
End Class


Public Interface ITestMock
Function TestFunc() As Object
End Interface
----------------

Alex McMahon

unread,
Oct 30, 2009, 9:59:52 AM10/30/09
to rhino...@googlegroups.com
Isn't this the same question you asked a little while ago? I think
it's maybe to do with you creating a stub rather than a mock... as
stubs aren't meant for asserting behaviour... (although I do recall a
grey area where they can be used like this)

2009/10/30 Tarpan <tarp...@gmail.com>:

Tarpan

unread,
Oct 30, 2009, 10:14:17 AM10/30/09
to Rhino.Mocks
Yes, it is the same question. I just did not get any answers last
time, so I'm trying again.

As for "you creating a stub rather than a mock",
Dim TestMock As ITestMock = MockRepository.GenerateMock(Of ITestMock)
()
does exactly the same.




On Oct 30, 9:59 am, Alex McMahon <fluxmu...@gmail.com> wrote:
> Isn't this the same question you asked a little while ago? I think
> it's maybe to do with you creating a stub rather than a mock... as
> stubs aren't meant for asserting behaviour... (although I do recall a
> grey area where they can be used like this)
>
> 2009/10/30 Tarpan <tarpa...@gmail.com>:

Tim Barcz

unread,
Oct 30, 2009, 10:32:43 AM10/30/09
to rhino...@googlegroups.com
Change GenerateMock to StrictMock...still pass?
--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Alex McMahon

unread,
Oct 30, 2009, 10:39:38 AM10/30/09
to rhino...@googlegroups.com
Works on my machine... I changed it to C# and the following works
(test fails). So maybe it's about it being in VB?

public interface ITestMock
{
object TestFunc();
}

public class TestClass
{
ITestMock testMock;

public TestClass(ITestMock testMock)
{
this.testMock = testMock;
}

public void test()
{
testMock.TestFunc();
testMock.TestFunc();
}
}

[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = MockRepository.GenerateMock<ITestMock>();
var target = new TestClass(mock);
target.test();
mock.AssertWasCalled(x => x.TestFunc(), o => o.Repeat.Once());
}
}

2009/10/30 Tarpan <tarp...@gmail.com>:

Tarpan

unread,
Oct 30, 2009, 10:54:11 AM10/30/09
to Rhino.Mocks
StrictMock requires a result for the TestFunc().
I'm not sure how to do it in AAA syntax - I have used record/playback
syntax before.

This
TestMock.Stub(Function(x) x.TestFunc()).Return(Nothing)
or this
Rhino.Mocks.SetupResult.For(TestMock.TestFunc).Return(Nothing)
or this
TestMock.Expect(Function(x) x.TestFunc()).Return(Nothing)
does not work with StrictMocks in AAA.

Tarpan

unread,
Oct 30, 2009, 10:56:11 AM10/30/09
to Rhino.Mocks
Does the test _fail_ on your machine?




On Oct 30, 10:39 am, Alex McMahon <fluxmu...@gmail.com> wrote:
> Works on my machine... I changed it to C# and the following works
> (test fails). So maybe it's about it being in VB?
>
> public interface ITestMock
>   {
>     object TestFunc();
>   }
>
>   public class TestClass
>   {
>     ITestMock testMock;
>
>     public TestClass(ITestMock testMock)
>     {
>       this.testMock = testMock;
>     }
>
>     public void test()
>     {
>       testMock.TestFunc();
>       testMock.TestFunc();
>     }
>   }
>
>   [TestClass]
>   public class UnitTest1
>   {
>     [TestMethod]
>     public void TestMethod1()
>     {
>       var mock = MockRepository.GenerateMock<ITestMock>();
>       var target = new TestClass(mock);
>       target.test();
>       mock.AssertWasCalled(x => x.TestFunc(), o => o.Repeat.Once());
>     }
>   }
>
> 2009/10/30 Tarpan <tarpa...@gmail.com>:

Alex McMahon

unread,
Oct 30, 2009, 10:59:16 AM10/30/09
to rhino...@googlegroups.com
Yes the test fails on my machine.

If I comment out one of the testMock.TestFunc() then it passes. Works
with both GenerateStub<> GenerateMock<> and GenerateStrictMock<> as
long as I add:

mock.Stub(x=>x.TestFunc()).Return(null);

early on.


I'm using Rhino Mocks 3.6 btw

2009/10/30 Tarpan <tarp...@gmail.com>:

Tarpan

unread,
Oct 30, 2009, 11:12:25 AM10/30/09
to Rhino.Mocks
Hmmmm.....Did I smoke a wrong weed this morning? :-)

It definitely does not work for me (the test passes).
Let me try 3.6 - maybe it was an issue in 3.5 and has been fixed.

Alex, thank you for your efforts.
BTW, I get more and more annoyed with VB. I don't like C# syntax
either, but still it looks simpler than VB.


On Oct 30, 10:59 am, Alex McMahon <fluxmu...@gmail.com> wrote:
> Yes the test fails on my machine.
>
> If I comment out one of the testMock.TestFunc() then it passes. Works
> with both GenerateStub<> GenerateMock<> and GenerateStrictMock<> as
> long as I add:
>
>  mock.Stub(x=>x.TestFunc()).Return(null);
>
> early on.
>
> I'm using Rhino Mocks 3.6 btw
>
> 2009/10/30 Tarpan <tarpa...@gmail.com>:

Alex McMahon

unread,
Oct 30, 2009, 11:35:46 AM10/30/09
to rhino...@googlegroups.com
I'm trying to write it in VB.net now to see if that's the problem...
and I'm struggling :)

I wonder if this page has a clue to the answer?
http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vb-net-rhino-mocks-3-5-and-lambda-expres

2009/10/30 Tarpan <tarp...@gmail.com>:

Tarpan

unread,
Oct 30, 2009, 12:00:58 PM10/30/09
to Rhino.Mocks
The VB's Lambda expressions screw-up is well-known for VB developers.
This is not the issue in this case.

Anybody use VB and Rhino 3.6? How to write this in 3.6? I just cannot
figure it out.
TestMock.AssertWasCalled(Function(x) x.TestFunc, Function(options)
options.Repeat.Once)

It worked fine in 3.5 but gives the "overload resolution failed" error
in 3.6.




On Oct 30, 11:35 am, Alex McMahon <fluxmu...@gmail.com> wrote:
> I'm trying to write it in VB.net now to see if that's the problem...
> and I'm struggling :)
>
> I wonder if this page has a clue to the answer?http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vb-net-rhino...
>
> 2009/10/30 Tarpan <tarpa...@gmail.com>:

Alex McMahon

unread,
Oct 30, 2009, 12:03:45 PM10/30/09
to rhino...@googlegroups.com
ahh good, it's not just me then :)

2009/10/30 Tarpan <tarp...@gmail.com>:

Tarpan

unread,
Oct 30, 2009, 2:19:35 PM10/30/09
to Rhino.Mocks
All right,
As far as I see Rhino 3.6 AssertWasCalled with cannot be used in VB
without some headache.

The problem is in the overload list:
AssertWasCalled (action as Sub....
AssertWasCalled (action as Function(Mock, Object)....

VB Lambda expressions can not do either - only AssertWasCalled (action
as Function(Mock).... is allowed (yes, stupid VB)

The only work around is use something like that:
TestMock.AssertWasCalled(New Action(Of ITestMock)(Function(x)
x.TestFunc()), Function(o) o.Repeat.twice)

Too bad, the overload list was more "VB-friendly" in 3.5.



On Oct 30, 12:03 pm, Alex McMahon <fluxmu...@gmail.com> wrote:
> ahh good, it's not just me then :)
>
> 2009/10/30 Tarpan <tarpa...@gmail.com>:

Tarpan

unread,
Oct 30, 2009, 2:28:01 PM10/30/09
to Rhino.Mocks
THANK YOU Everybody.

My findings:
"Repeat" does not work in 3.5, but works fine in 3.6.
There is other issues in 3.6 if used in VB (see my message above).
Those issue are more VB issues then RhinoMocks.

Reply all
Reply to author
Forward
0 new messages