|
From: Vaughn, C. <Va...@va...> - 2003-08-01 15:29:18
|
Hello,
I have a test fixture that's causing me trouble. Can someone look at it and
provide some comments. In general, I have problems returning value types
and calling SetupResult() in various cases.
using System;
using NUnit.Framework;
using NMock;
using NMock.Dynamic;
using NMock.Constraints;
//using Explorer.Gui;
//using System.Windows.Forms;
namespace Explorer.Tests
{
/// <summary>
/// Summary description for NMockTest.
/// </summary>
[TestFixture]
public class NMockTest
{
public NMockTest()
{
}
bool m_test = false;
private void eTester( object sender, EventArgs e )
{
m_test = true;
}
public interface IBar
{
string Age { get; }
string BaseCall();
}
public interface IFoo : IBar
{
string Name { get; set; }
event EventHandler Test;
void Bar();
void Bar( string text );
}
public void TestSetupResultAndExpect()
{
DynamicMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.SetupResult( "Name", "bob" );
foo.Expect( "Name", "bill" );
System.Diagnostics.Debug.Write( ifoo.Name );
ifoo.Name = "bill";
foo.Verify();
}
public void TestExpectAndReturnAndExpect()
{
DynamicMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.ExpectAndReturn( "Name", "bob" );
foo.Expect( "Name", "bill" );
System.Diagnostics.Debug.Write( ifoo.Name );
ifoo.Name = "bill";
foo.Verify();
}
public void TestBasePropertySetupResult()
{
IMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.SetupResult( "Age", "bob" );
System.Diagnostics.Debug.Write( ifoo.Age);
foo.Verify();
}
public void TestBasePropertyExpectAndReturn()
{
IMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.ExpectAndReturn( "Age", "bob" );
System.Diagnostics.Debug.Write( ifoo.Age );
foo.Verify();
}
[ExpectedException(typeof(VerifyException))]
public void TestPropertyRetTypeFailExpected()
{
DynamicMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.SetupResult( "Age", 1 );
System.Diagnostics.Debug.Write( ifoo.Age);
foo.Verify();
}
public void TestBaseMethodSetupResult()
{
DynamicMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.SetupResult( "BaseCall", "bob" );
System.Diagnostics.Debug.Write( ifoo.BaseCall() );
foo.Verify();
}
[ExpectedException(typeof(VerifyException))]
public void TestMethodRetTypeFailExpected()
{
DynamicMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.SetupResult( "BaseCall", 1 );
System.Diagnostics.Debug.Write( ifoo.BaseCall() );
foo.Verify();
}
public void TestEvent()
{
IMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
CollectingConstraint h = new CollectingConstraint();
foo.Expect( "add_Test", h );
ifoo.Test += new EventHandler( eTester );
((EventHandler)h.Parameter)( null, null );
Assertion.AssertEquals( "Event", true, m_test );
foo.Verify();
}
// this test should fail
[ExpectedException(typeof(VerifyException))]
public void TestBarFailExpected()
{
DynamicMock foo = new DynamicMock( typeof( IFoo ) );
IFoo ifoo = ((IFoo)foo.MockInstance);
foo.Expect( "Bar" );
ifoo.Bar( "test" );
foo.Verify();
}
public interface IFoo2
{
System.Drawing.Color color { get; }
DateTime date { get; }
}
public void TestColor()
{
IMock foo = new DynamicMock( typeof( IFoo2 ), "bob"
);
foo.ExpectAndReturn( "color",
System.Drawing.Color.Black );
foo.ExpectAndReturn( "date", DateTime.Now.Date );
System.Drawing.Color c =
((IFoo2)foo.MockInstance).color;
DateTime d = ((IFoo2)foo.MockInstance).date;
Assertion.AssertEquals( "color",
System.Drawing.Color.Black, c );
Assertion.AssertEquals( "date", DateTime.Now.Date, d
);
}
}
}
Also, one of the tests I submitted would pass if you change the
initialization of "ignore" from one of the constructors and just do it at
declaration time like so:
public class DynamicMock : Mock
{
private object obj;
private Type type;
private IList ignore= new ArrayList();
...
}
And finally, I think if you should provide an "Expect..." function that will
ignore parameters, versus having Expect() ignore and check. the combined
nature of the current Expect() confuses my "TestBarFailExpected"
thanx
cliff
This message may have included proprietary or protected information. This
message and the information contained herein are not to be further
communicated without my express written consent.
|