tkx in advance
Paulo Aboim Pinto
my first approach was this
[Test]
[ExpectedException(typeof(NullReferenceException))]
public void SavePostNullException()
{
// setup the PostSaved event
postEditViewMock.PostSaved += null;
IEventRaiser postSavedRaiser =
LastCall.IgnoreArguments().GetEventRaiser();
// record the expectation of call of this method
blogDataServiceMock.SavePost (null);
mocks.ReplayAll();
new PostPresenter(postEditViewMock, blogDataServiceMock);
postSavedRaiser.Raise(postEditViewMock, EventArgs.Empty);
}
With this test I want to raise an event and check if inside the method
SavePost the parameter is NULL and if it's null I want to throw an
exception.
I have my mocks.VerifyAll() in the TearDown method and this is the
error I get
Failures:
1) UnitTest.Tests.TestClass.SavePostNullException : TearDown :
System.InvalidOperationException : This action is invalid when the
mock object is in record state.
--TearDown
at Rhino.Mocks.Impl.RecordMockState.get_VerifyState () [0x00000]
at Rhino.Mocks.MockRepository.Verify (System.Object obj) [0x00000]
at Rhino.Mocks.MockRepository.VerifyAll () [0x00000]
at UnitTest.Tests.TestClass.TearDown () [0x00000] in /home/esqueleto/
Projects/MbUnitTest/UnitTest.Tests/TestClass.cs:34
at <0x00000> <unknown method>
at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke (object,object[])
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00056] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:146
ok .. I'm recording the expectation befores de ReplayAll(), yes? Why
he is saying that?
tkx in advance
Paulo Aboim Pinto
Odivelas - Portugal
If I don't use the [ExpectedException ... ] I get this:
1) UnitTest.Tests.TestClass.SavePostNullException :
System.NullReferenceException : Post object cannot be null
TearDown : System.InvalidOperationException : This action is invalid
when the mock object is in record state.
at UnitTest.Controller.BlogDataService.SavePost
(UnitTest.Controller.Post post) [0x00012] in /home/esqueleto/Projects/
MbUnitTest/UnitTest.Controller/Service/BlogDataService.cs:16
at UnitTest.Tests.TestClass.SavePostNullException () [0x00017] in /
home/esqueleto/Projects/MbUnitTest/UnitTest.Tests/TestClass.cs:62
at <0x00000> <unknown method>
at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke (object,object[])
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00056] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:146
--TearDown
at Rhino.Mocks.Impl.RecordMockState.get_VerifyState () [0x00000]
at Rhino.Mocks.MockRepository.Verify (System.Object obj) [0x00000]
at Rhino.Mocks.MockRepository.VerifyAll () [0x00000]
at UnitTest.Tests.TestClass.TearDown () [0x00000] in /home/esqueleto/
Projects/MbUnitTest/UnitTest.Tests/TestClass.cs:34
at <0x00000> <unknown method>
at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke (object,object[])
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00056] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:146
what I want is to test that this method is been thrown.
tkx in advace
Paulo Aboim Pinto
Odivelas - Portugal
On Aug 7, 4:44 am, "David Laribee" <lari...@gmail.com> wrote:
> theUnit.ThisMethodHadBetterThrowAnException();
Failures:
1) UnitTest.Tests.TestClass.SavePostNullException :
System.NullReferenceException was expected
I try to use the other way of testing the Exception
[Test]
public void SavePostNullException()
{
// setup the PostSaved event
postEditViewMock.PostSaved += null;
IEventRaiser postSavedRaiser =
LastCall.IgnoreArguments().GetEventRaiser();
// record the expectation of call of this method
Expect.Call(blogDataServiceMock.SavePost(null)).Throw(new
NullReferenceException);
mocks.ReplayAll();
new PostPresenter(postEditViewMock, blogDataServiceMock);
postSavedRaiser.Raise(postEditViewMock, EventArgs.Empty);
mocks.VerifyAll();
}
LIke the example made by David Laribee, but even with this I get error
and cannot compile it. At this line I get the error
Expect.Call(blogDataServiceMock.SavePost(null)).Throw(new
NullReferenceException);
[Task:File=/home/esqueleto/Projects/MbUnitTest/UnitTest.Tests/
TestClass.cs, Line=64, Column=79, Type=Error, Priority=Normal,
Description=A new expression requires () or [] after type(CS1526)]
If I do
Expect.Call(blogDataServiceMock.SavePost(null)).Throw(new
NullReferenceException());
I get the errors
[Task:File=/home/esqueleto/Projects/MbUnitTest/UnitTest.Tests/
TestClass.cs, Line=64, Column=32, Type=Error, Priority=Normal,
Description=Cannot convert type `void' to `object'(CS1503: Argument
1)]
[Task:File=/home/esqueleto/Projects/MbUnitTest/UnitTest.Tests/
TestClass.cs, Line=64, Column=32, Type=Error, Priority=Normal,
Description=The best overloaded method match for
`Rhino.Mocks.Expect.Call(object)' has some invalid arguments(CS1502)]
How can I expect that some expectation is throw by a method that I'm
testing?
tkx in advace
Paulo Aboim Pinto
Odivelas - Portugal
On Aug 7, 7:10 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> Looks like blogDataServiceMock.SavePost() is not a virtual method
>
[Test]
[ExpectedException(typeof(NullReferenceException))]
public void SavePostNullException()
{
// setup the PostSaved event
postEditViewMock.PostSaved += null;
IEventRaiser postSavedRaiser =
LastCall.IgnoreArguments().GetEventRaiser();
mocks.ReplayAll();
new PostPresenter(postEditViewMock, blogDataServiceMock);
postSavedRaiser.Raise(postEditViewMock, EventArgs.Empty);
mocks.VerifyAll();
}
The error is:
Failures:
1) UnitTest.Tests.TestClass.SavePostNullException : An unexpected
exception type was thrown
Expected: System.NullReferenceException
but was: System.Reflection.TargetInvocationException
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00083] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:156
at System.Reflection.MethodBase.Invoke (System.Object obj,
System.Object[] parameters) [0x00000] in /home/esqueleto/myTrash/
MonoInstall/mcs/class/corlib/System.Reflection/MethodBase.cs:93
at System.Delegate.DynamicInvokeImpl (System.Object[] args)
[0x000b6] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System/Delegate.cs:370
at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args)
[0x00018] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System/MulticastDelegate.cs:71
at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000]
in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/System/
Delegate.cs:342
at Rhino.Mocks.Impl.EventRaiser.Raise (System.Object[] args)
[0x00000]
at UnitTest.Tests.TestClass.SavePostNullException () [0x00034] in /
home/esqueleto/Projects/MbUnitTest/UnitTest.Tests/TestClass.cs:65
at <0x00000> <unknown method>
at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke (object,object[])
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00056] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:146
If I that the [ExpectedException(typeof(NullReferenceException))] tag
the error is:
Failures:
1) UnitTest.Tests.TestClass.SavePostNullException :
System.Reflection.TargetInvocationException : Exception has been
thrown by the target of an invocation.
----> System.NullReferenceException : Post object cannot be null
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00083] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:156
at System.Reflection.MethodBase.Invoke (System.Object obj,
System.Object[] parameters) [0x00000] in /home/esqueleto/myTrash/
MonoInstall/mcs/class/corlib/System.Reflection/MethodBase.cs:93
at System.Delegate.DynamicInvokeImpl (System.Object[] args)
[0x000b6] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System/Delegate.cs:370
at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args)
[0x00018] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System/MulticastDelegate.cs:71
at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000]
in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/System/
Delegate.cs:342
at Rhino.Mocks.Impl.EventRaiser.Raise (System.Object[] args)
[0x00000]
at UnitTest.Tests.TestClass.SavePostNullException () [0x00034] in /
home/esqueleto/Projects/MbUnitTest/UnitTest.Tests/TestClass.cs:65
at <0x00000> <unknown method>
at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke (object,object[])
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00056] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:146
--NullReferenceException
at UnitTest.Controller.BlogDataService.SavePost
(UnitTest.Controller.Post post) [0x00026] in /home/esqueleto/Projects/
MbUnitTest/UnitTest.Controller/Service/BlogDataService.cs:22
at UnitTest.Controller.PostPresenter.Save () [0x00000] in /home/
esqueleto/Projects/MbUnitTest/UnitTest.Controller/Service/
PostPresenter.cs:30
at UnitTest.Controller.PostPresenter.onPostSaved (System.Object
sender, System.EventArgs e) [0x0000a] in /home/esqueleto/Projects/
MbUnitTest/UnitTest.Controller/Service/PostPresenter.cs:38
at <0x00000> <unknown method>
at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke (object,object[])
at System.Reflection.MonoMethod.Invoke (System.Object obj,
BindingFlags invokeAttr, System.Reflection.Binder binder,
System.Object[] parameters, System.Globalization.CultureInfo culture)
[0x00056] in /home/esqueleto/myTrash/MonoInstall/mcs/class/corlib/
System.Reflection/MonoMethod.cs:146
This is ok .. but the exception is not been tested. Maybe something is
wrong with the interfaces and the implementations?
tkx in advace
Paulo Aboim Pinto
Odivelas - Portugal
On Aug 7, 7:10 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> Looks like blogDataServiceMock.SavePost() is not a virtual method
>
> > > using (mockRepository.Record ())
> > > {
> > > Expect.Call(mockService.MethodThatThrowsException()).Throw(new
> > > MyCustomException);
>
> > > }
>
> > > using (mockRepository.Playback ())
> > > {
http://www.olimpocms.com/UsingRhinoMock.zip
tkx in advance
Paulo Aboim Pinto
Odivelas - Portugal
Definitively was my problem to write Unit Tests. I thought that I need
all the time to run the test through the Presenter. Of course I can
run in every layers.
can you say what is the Replay State. I can't find anything about this
state.
tkx in advance
Paulo Aboim Pinto
Odivelas - Portugal