Hello everybody,
I face to such a problem that that raged in my head. So I need your help to solve it.
I've a user control and a method
Now I've to unit test for this method (
I face to such a problem that that raged in my head. So I need your help to solve it.
I've a user control and a method
CalculatePreviewArea
like that:public
partial
class
Control_Inside : UserControl
{
//some properties...
//(private) method1()
//(private) method2()
private
void
CalculatePreviewArea(
out
double
width,
out
double
height)
{
//some logic here...
//FillArea is dependency property of User Control
width = FillArea.Width;
height =FillArea.Height;
double
Sys_Width = SystemParameters.WorkArea.Width;
double
Sys_Height = SystemParameters.WorkArea.Height;
Window window = Application.Current.MainWindow;
Point location_From_Screen = window.PointToScreen(
new
Point(0.0, 0.0));
if
(location_From_Screen.X + width - Fill_Area.X >= Sys_Width)
width =Sys_Width + (location_From_Screen.X - Fill_Area.X);
//....
}
CalculatePreviewArea
) using JustMock. I created a mock user control , I also set FillArea value successfull, but there is no way to access Application.Current.Windows. When I debug the test,it always return null. I need your helps to test this, thanks.
7 Answers, 1 is accepted
0
Hello Ninh,
Thank you for contacting Telerik support.
Is it possible to wrap the method under test into a compiling project and send it over for further investigation? This will allow us to provide a possible solution much faster.
Thank you in advance.
Regards,
Kaloyan
Telerik
Thank you for contacting Telerik support.
Is it possible to wrap the method under test into a compiling project and send it over for further investigation? This will allow us to provide a possible solution much faster.
Thank you in advance.
Regards,
Kaloyan
Telerik
0

Ninh
Top achievements
Rank 1
answered on 25 May 2013, 02:38 AM
Hello Kaloyan
Thanks to your answer. I think's it's difficult to wrap the method under test into a compiling project before sending to you. But I should tell you the fundemental idea of my project. I developed a UserControl( Control_Inside) with its dependency property and method inside. After that, I put in in a RadDocking pane (in a RadDocking Control, also be main application).
When the pane changing state(docking, floating, pin...) I try to get the position relatively from it to main window application (RadDocking). The
Although I speak a bite prolix, but the main difficult for a test is there be no way to set the parameter (include Application.Current.Windows) to the test method. Need your support to solve it. Thanks.
Thanks to your answer. I think's it's difficult to wrap the method under test into a compiling project before sending to you. But I should tell you the fundemental idea of my project. I developed a UserControl( Control_Inside) with its dependency property and method inside. After that, I put in in a RadDocking pane (in a RadDocking Control, also be main application).
When the pane changing state(docking, floating, pin...) I try to get the position relatively from it to main window application (RadDocking). The
CalculatePreviewArea void is
used to calculate two sizes of a Rectangle which has been drawed in document host . This Rectangle is fit into the relative position of Radpane (contain UC) and main Application.Although I speak a bite prolix, but the main difficult for a test is there be no way to set the parameter (include Application.Current.Windows) to the test method. Need your support to solve it. Thanks.
0
Hello Ninh,
Thank you for the explanation. Still there is a possibility that I have misunderstood you. Please, correct me if this is the case.
I made a test for you, in which I have a predefined "Window" with some properties set. Then, I am mocking the "Application.Current.MainWindow" return value to return my predefined window. Finally, I assert my actual window has its properties set as my predefined window:
This test is passing as expected. Note that, with the commercial version of JustMock, you should not have any problems mocking the Application.Current.MainWindow.
Please, let me know if there is anything else I could help you with.
Regards,
Kaloyan
Telerik
Thank you for the explanation. Still there is a possibility that I have misunderstood you. Please, correct me if this is the case.
I made a test for you, in which I have a predefined "Window" with some properties set. Then, I am mocking the "Application.Current.MainWindow" return value to return my predefined window. Finally, I assert my actual window has its properties set as my predefined window:
[TestMethod]
public
void
ShouldReturnFakeWindow()
{
// Predefined window
Window expectedWindow =
new
Window();
expectedWindow.Width = 25;
expectedWindow.Height = 50;
// Arrange
Mock.Arrange(() => Application.Current.MainWindow).Returns(expectedWindow);
// Act
var actualWindow = Application.Current.MainWindow;
// Assert
Assert.AreEqual(25, actualWindow.Width);
Assert.AreEqual(50, actualWindow.Height);
}
Please, let me know if there is anything else I could help you with.
Regards,
Kaloyan
Telerik
0

Ninh
Top achievements
Rank 1
answered on 28 May 2013, 10:24 AM
Hello Kaloyan
when I try to use your code in my project, and debug the test from the beginning, I got an error. You can check the error by the image that I've attached. Thanks again.
when I try to use your code in my project, and debug the test from the beginning, I got an error. You can check the error by the image that I've attached. Thanks again.
0
Hi Ninh,
The example I sent is written against JustMock 2013 Q2 Internal Build (2013.2.527). I strongly recommend updating to it. You can download the .msi from here.
However, as there is a bug in JustMock 2013 Q1 SP2 (your current version), I changed the test like this:
This should pass.
Please, let me know if there is anything else I could help you with.
Regards,
Kaloyan
Telerik
The example I sent is written against JustMock 2013 Q2 Internal Build (2013.2.527). I strongly recommend updating to it. You can download the .msi from here.
However, as there is a bug in JustMock 2013 Q1 SP2 (your current version), I changed the test like this:
[TestMethod]
public
void
TestMethod1()
{
var myApplication =
new
Application();
myApplication.MainWindow =
new
Window()
{
Width = 25,
Height = 50
};
// Arrange
Mock.Arrange(() => Application.Current).Returns(myApplication);
// Act
var actualWindow = Application.Current.MainWindow;
// Assert
Assert.AreEqual(25, actualWindow.Width);
Assert.AreEqual(50, actualWindow.Height);
}
Please, let me know if there is anything else I could help you with.
Regards,
Kaloyan
Telerik
0

Ninh
Top achievements
Rank 1
answered on 31 May 2013, 08:02 AM
OH, it works. Thanks for all your help
0
Hi Ninh,
I am glad I could help you.
Contact us again if you need further assistance.
Regards,
Kaloyan
Telerik
I am glad I could help you.
Contact us again if you need further assistance.
Regards,
Kaloyan
Telerik