chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Tested Class:
public class DragHandle
{
private IGBCanvasService m_gbCanvasService;
private IEditorPaneControl m_iEditorPaneControl;
private Path m_dragHandle;
private Size m_dragHandleSize;
private double m_offsetX;
private double m_offsetY;
private Point m_downLeftTopPoint;

public void OnMouseMove(Point argPoint)
{
Point topLeftPoint = new Point(argPoint.X - this.m_offsetX,
argPoint.Y - this.m_offsetY);

Point topLeftToGridPoint = this.m_gbCanvasService.SnapToGrid(topLeftPoint,
this.m_dragHandleSize);

Point topLeftToPicturePoint = this.m_gbCanvasService.SnapToPicture(topLeftToGridPoint,
this.m_dragHandleSize);

if(this.m_dragHandle.Data == null)
{
this.m_iEditorPaneControl.LogMessage("The move is normally started.");

this.m_gbCanvasService.AddHandle(this.m_dragHandle);
}

this.m_dragHandle.Data = this.CreateDragGeometry(topLeftToPicturePoint,
this.m_dragHandleSize);
}

private RectangleGeometry CreateDragGeometry(Point argTopLeftPoint, Size argRectSize)
{
Rect dragRect = new Rect(argTopLeftPoint, argRectSize);

RectangleGeometry dragGeometry = new RectangleGeometry(dragRect);

return dragGeometry;
}
}

TestClass:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TypeMock;
using Com.Yamatake.J5.Screen.GraphicBuilder.BasicShapeComponents.Controls;
using System.Windows;
using Com.Yamatake.J5.Screen.GraphicBuilder.GBObjectInterface;
using System.Windows.Shapes;
using System.Windows.Media;
using Com.Yamatake.J5.Screen.GraphicBuilder.BasicShapeComponents.Views;

namespace NunitBasicShapeComponents
{
[TestFixture]
public class DragHandleTest
{
[SetUp]
public void Initialize()
{
MockManager.Init();
}

[TearDown]
public void Finish()
{
MockManager.Verify();
}


[Test]
public void OnMouseMoveTest1()
{
CrossThreadTestRunner runner = new CrossThreadTestRunner();
runner.RunInSTA(
delegate
{
DragHandle handle = new DragHandle();
ObjectState dragHandleObjectState = new ObjectState(handle);

MockObject gbCanvasMock = MockManager.MockObject(typeof(IGBCanvasService));
MockObject editorPaneControlMock = MockManager.MockObject(typeof(IEditorPaneControl));
Path dragPath = new Path();

dragHandleObjectState.SetField("m_offsetX", 10);
dragHandleObjectState.SetField("m_offsetY", 20);
dragHandleObjectState.SetField("m_dragHandleSize", new Size(40, 60));
dragHandleObjectState.SetField("m_gbCanvasService", (IGBCanvasService)gbCanvasMock.Object);
dragHandleObjectState.SetField("m_dragHandle", dragPath);
dragHandleObjectState.SetField("m_iEditorPaneControl", (IEditorPaneControl)editorPaneControlMock.Object);


gbCanvasMock.ExpectAndReturn("SnapToGrid", new Point(50, 90)).Args(new Point(50, 90), new Size(40, 60));
gbCanvasMock.ExpectAndReturn("SnapToPicture", new Point(50, 90)).Args(new Point(50, 90), new Size(40, 60));
editorPaneControlMock.ExpectCall("LogMessage").Args("The move is normally started.");
gbCanvasMock.ExpectCall("AddHandle").Args(dragPath);

handle.OnMouseMove(new Point(60, 110));

Rect dragRect = (Rect)((RectangleGeometry)dragPath.Data).Rect;
Assert.AreEqual(new Rect(50, 90, 40, 60), dragRect);
});
}

[Test]
public void OnMouseMoveTest2()
{
CrossThreadTestRunner runner = new CrossThreadTestRunner();
runner.RunInSTA(
delegate
{
IGBCanvasService gbCanvasMockObject = (IGBCanvasService)RecorderManager.CreateMockedObject(typeof(IGBCanvasService));
IEditorPaneControl editorPaneControlMockObject = (IEditorPaneControl)RecorderManager.CreateMockedObject(typeof(IEditorPaneControl));
Path dragPath = new Path();
dragPath.Data = new RectangleGeometry(new Rect(40, 80, 40, 60));

DragHandle handle = new DragHandle();
ObjectState dragHandleObjectState = new ObjectState(handle);

dragHandleObjectState.SetField("m_offsetX", 10);
dragHandleObjectState.SetField("m_offsetY", 20);
dragHandleObjectState.SetField("m_downLeftTopPoint", new Point(20, 30));
dragHandleObjectState.SetField("m_dragHandleSize", new Size(40, 60));
dragHandleObjectState.SetField("m_gbCanvasService", gbCanvasMockObject);
dragHandleObjectState.SetField("m_dragHandle", dragPath);
dragHandleObjectState.SetField("m_iEditorPaneControl", editorPaneControlMockObject);

using(RecordExpectations recorder = new RecordExpectations())
{
gbCanvasMockObject.SnapToGrid(new Point(50, 90), new Size(40, 60));
recorder.Return(new Point(50, 90)).CheckArguments();
gbCanvasMockObject.SnapToPicture(new Point(50, 90), new Size(40, 60));
recorder.Return(new Point(50, 90)).CheckArguments();
}

handle.OnMouseMove(new Point(60, 110));

Rect dragRect = (Rect)((RectangleGeometry)dragPath.Data).Rect;
Assert.AreEqual(new Rect(50, 90, 40, 60), dragRect);
});
}
}
}

OnMouseMoveTest1 is success,but OnMouseMoveTest2 is failed.
The error message is:
estCase 'NunitBasicShapeComponents.DragHandleTest.OnMouseMoveTest2'
failed: TypeMock.VerifyException :
TypeMock Verification: Call to Com.Yamatake.J5.Screen.GraphicBuilder.GBObjectInterface.IGBCanvasService.SnapToGrid() Parameter: 1
expected: <0>
but was: <50>
TearDown : System.Reflection.TargetInvocationException : ŒÄ‚Ñ
asked by xuyingying (1.2k points)

2 Answers

0 votes
If the check number is out of the using block ,it will work.

Point point = new Point(50, 90);
Size size = new Size(40, 60);

using(RecordExpectations recorder = new RecordExpectations())
{
gbCanvasMockObject.SnapToGrid(point, size);
recorder.Return(point).CheckArguments();
gbCanvasMockObject.SnapToPicture(point, size);
recorder.Return(point).CheckArguments();
}
I hope this will help someone.I don't know if it is a bug.
answered by xuyingying (1.2k points)
0 votes
Hi

The problem is that when you write:
gbCanvasMockObject.SnapToGrid(new Point(50, 90), new Size(40, 60));

You are creating two expectations for Point constructor.
This is different than the line:
recorder.Return(new Point(50, 90)).CheckArguments();

In the line above the Isolator 'knows' that you want to use the Point as the return value since it's inside the recorder.Return() statement so it will not create an expectation for the Point constructor.
answered by ohad (35.4k points)
...