Need some helps

8 posts, 0 answers
  1. Ninh
    Ninh avatar
    6 posts
    Member since:
    Oct 2012

    Posted 22 May 2013 Link to this post

    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 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);
                 
                //....
               }
    Now I've to unit test for this method (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.
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 24 May 2013 Link to this post

    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
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Ninh
    Ninh avatar
    6 posts
    Member since:
    Oct 2012

    Posted 24 May 2013 Link to this post

    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 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.
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 28 May 2013 Link to this post

    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:
    [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);
    }
    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
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  6. Ninh
    Ninh avatar
    6 posts
    Member since:
    Oct 2012

    Posted 28 May 2013 Link to this post

    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.
  7. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 30 May 2013 Link to this post

    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:
    [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);
    }
    This should pass.

    Please, let me know if there is anything else I could help you with.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  8. Ninh
    Ninh avatar
    6 posts
    Member since:
    Oct 2012

    Posted 31 May 2013 Link to this post

    OH, it works. Thanks for all your help
  9. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 31 May 2013 Link to this post

    Hi Ninh,

    I am glad I could help you.

    Contact us again if you need further assistance.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top
DevCraft R3 2016 release webinar banner