I need to clarify something. I got the exception when I run the test
with debugger.
You are right, without debugger test succeeds.
So I added some more complexity to the tested class. Here is the code:
public interface IInnerIndexer
{
string this[string index]
{
get;
}
}
public interface IIndexer
{
IInnerIndexer this[string index]
{
get;
}
}
[TestFixture]
public class TestApplication
{
private MockRepository m_Mocks;
[SetUp]
public void Setup()
{
m_Mocks = new MockRepository();
}
[Test]
public void CheckIndexer()
{
IIndexer indexerObj = m_Mocks.StrictMock<IIndexer>();
IInnerIndexer innerIndexerObj =
m_Mocks.StrictMock<IInnerIndexer>();
using (m_Mocks.Record())
{
Expect.Call(indexerObj["index1"]).Return
(innerIndexerObj);
Expect.Call(innerIndexerObj["index2"]).Return
("indexValue");
}
using (m_Mocks.Playback())
{
Console.WriteLine(indexerObj["index1"]);
}
}
}
I get the following exception with the debugger:
Previous method 'IInnerIndexer.get_Item("index2");' requires a return
value or an exception to throw.
at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
at Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation
invocation, MethodInfo method, Object[] args)
at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation,
Object proxy, MethodInfo method, Object[] args)
at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation
invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at IInnerIndexerProxybdd79bbf3db24cfcb0ede5cd17fdcda2.get_Item(String
index)
And I get the following exception without debugger:
Rhino.Mocks.Exceptions.ExpectationViolationException :
IInnerIndexer.get_Item("index2"); Expected #1, Actual #0.
So the question is what's wrong with this code?
Thanks for your help
Slavik
On Jan 28, 2:31 pm, Tim Barcz <[email protected]> wrote:
> The code I pasted works. I actually put in compiler and it passes.
>
>
>
>
>
> On Thu, Jan 28, 2010 at 5:48 AM, Tim Barcz <[email protected]> wrote:
> > You're missing a return on your expectation.
>
> > using (m_Mocks.Record())
> > {
> > Expect.Call(indexerObj["index1"]).*Return("hello")*;
> > }
>
> > using(m_Mocks.Playback())
> > {
> > Console.WriteLine(indexerObj["index1"]);
>
> > }
>
> > On Thu, Jan 28, 2010 at 4:51 AM, slavlenty <[email protected]> wrote:
>
> >> I am trying to mock a class with an indexer property
> >> Here is the example of the code:
>
> >> public interface IIndexer
> >> {
> >> string this[string index]
> >> {
> >> get;
> >> }
> >> }
> >> [TestFixture]
> >> public class TestApplication
> >> {
> >> private MockRepository m_Mocks;
> >> [SetUp]
> >> public void Setup()
> >> {
> >> m_Mocks = new MockRepository();
> >> }
> >> [Test]
> >> public void CheckIndexer()
> >> {
> >> IIndexer indexerObj = m_Mocks.StrictMock<IIndexer>();
>
> >> using (m_Mocks.Record())
> >> {
> >> Expect.Call(indexerObj["index1"]);
> >> }
> >> }
> >> }
> >> There is an exception when I'm calling to Expect.Call(indexerObj
> >> ["index1"]);
> >> The exception is the following:
>
> >> TestCase 'GSM.DataAccess.MDALAccess.Test.TestApplication.CheckIndexer'
> >> failed: System.InvalidOperationException : Previous method
> >> 'IIndexer.get_Item("index1");' requires a return value or an exception
> >> to throw.
> >> at Rhino.Mocks.Impl.RecordMockState.AssertPreviousMethodIsClose()
> >> at Rhino.Mocks.Impl.RecordMockState.MethodCall(IInvocation
> >> invocation, MethodInfo method, Object[] args)
> >> at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation,
> >> Object proxy, MethodInfo method, Object[] args)
> >> at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation
> >> invocation)
> >> at Castle.DynamicProxy.AbstractInvocation.Proceed()
> >> at IIndexerProxyc2c7135b1bea490d8bc099fa605d11c4.get_Item(String
> >> index)
>
> >> How I can mock the call IIndexer.get_Item("index1")?
>
> >> Thanks
> >> Slavik
>
> >> --
> >> 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]<rhinomocks%2bunsubscr...@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
--
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.