This is a migrated thread and some comments may be shown as answers.

Error while trying to run justmock sample

15 Answers 174 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sathyamurthy
Top achievements
Rank 1
Sathyamurthy asked on 31 Jul 2012, 10:48 AM
Hi,
     I downloaded the trial version of Justmock. I'm trying to run the tests in HttpContextTest.cs. I'm getting the following error while running it. Here is the code it's trying to run. JustMock was enabled in the menu.
[TestClass]
    public class HttpContextTest
    {
       static HttpContextTest()
        {
            Mock.Partial<HttpContext>().For<HttpContext, HttpRequest>(x => x.Request);
            Mock.Partial<HttpContext>().For(() => HttpContext.Current);
        }
        [TestMethod]
        public void ShouldMockCurrentHtppContext()
        {
            bool called = false;
            Mock.Arrange(() => HttpContext.Current).DoInstead(() => called = true);
            var ret = HttpContext.Current;
            Assert.True(called);
        }
}


Test 'Telerik.JustMock.Tests.Elevated.Integration.HttpContextTest.ShouldMockCurrentHtppContext' failed: Telerik.JustMock.MockException : There were some problems intercepting the mock call. Optionally, please make sure that you have turned on JustMock's profiler while mocking concrete members.
at Telerik.JustMock.Expectations.Expectation.ThrowForInvalidCall(IInvocation invocation)
at Telerik.JustMock.Expectations.Expectation.Process[TResult](IInvocation invocation)
at Telerik.JustMock.Mock.<>c__DisplayClass1`1.<Arrange>b__0(MockContext`1 x)
at Telerik.JustMock.MockContext.Setup[TDelgate,TReturn](Instruction instruction, Func`2 function)
at Telerik.JustMock.Mock.Arrange[TResult](Expression`1 expression)
Elevated\Integration\HttpContextTest.cs(32,0): at Telerik.JustMock.Tests.Elevated.Integration.HttpContextTest.ShouldMockCurrentHtppContext()

15 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 03 Aug 2012, 08:33 PM
Hi Sathyamurthy,

Thanks again for contacting us.

From Q2 2012 you no longer have to do Mock.Partial to mock HttpContext class. I wrote the test in the following way and it working as expected:

[TestMethod]
public void ShouldMockCurrentHtppContext()
{
    bool called = false;
    Mock.Arrange(() => HttpContext.Current).DoInstead(() => called = true);
    var ret = HttpContext.Current;
    Assert.IsTrue(called);
}

Here to note that we have upgraded the profiler in Q2 2012 therefore if you are using the latest Telerik.JustMock.DLL please check that your profiler is updated as well and vice-versa. The best way it is to do a fresh install and update all your project references for JustMock. 

 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Sathyamurthy
Top achievements
Rank 1
answered on 06 Aug 2012, 11:45 AM
I have the Q2 2012 version. I figured out the issue. I had installed TypeMock isolator and it was enabled. This test started working after I disabled the Typemock Isolator. However, the tests that I wrote using Justmock were failing even after I disabled Typemock Isolator. I had remove the references to Typemock Isolator dll from my Test project to get my tests working. This is something you may want to look into.
I also figured out another issue. Justmock is not working when I use [TestCase] attribute instead of [Test] attribute to define my test methods. I use NUnit.    
0
Ricky
Telerik team
answered on 06 Aug 2012, 05:21 PM
Hi Sathyamurthy,

Thanks again for contacting us. JustMock will not work properly if TypeMock is installed since both tools utilize profiler and one of them overrides the other if both are installed.

We have noted the issue with TestCase attribute and will include the fix in the upcoming SP1 release.

 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Sathyamurthy
Top achievements
Rank 1
answered on 07 Aug 2012, 07:22 AM
Thanks for the response. I have found an issue with mocking DateTime.Now(may be this is applicable for all mscorlib mocking).  Taking the example given below, assuming that GetTime takes an argument of reference type which is defined in another assembly, it's failing. I mean mocking is not happening. Rather, DateTime.Now is actually called. I'm not sure if I'm missing something. Consider the following code. Assuming that "User" class is defined in a different assembly, the mocking is failing.

public class Time
{
    public DateTime GetTime(User user)
    {
        return DateTime.Now;
    }
}

Unit Test:

[TestFixture]
public class DateTimeTest
{
    [Test]
    public void GetTime_Returns_Current_Date()
    {
        DateTime expectedTime = new DateTime(2011, 1, 1);
        User user= new User();
        Mock.Replace(() => DateTime.Now).In<Time>(x => x.GetTime(user));

        Mock.Arrange(() => DateTime.Now).Returns(expectedTime);
        Time time = new Time();
        DateTime now = time.GetTime(user);

        Assert.IsTrue(now.Year == 2011);
        Assert.IsTrue(now.Month == 1);
        Assert.IsTrue(now.Day == 1);
    }

} 
0
Ricky
Telerik team
answered on 08 Aug 2012, 05:54 PM
Hi Sathyamurty,

Thanks again for contacting us.

We have identified that the issue is due to loading of User object that is located in another assembly, which is not strongly typed. However, we just the fixed issue and it will be available later this week.

In addition, you can also write the test in the following way that works nicely:


[Test]
    public void GetTime_Returns_Current_Date()
    {
        DateTime expectedTime = new DateTime(2011, 1, 1);
        User user= new User();
         
        Mock.Arrange(() => time.GetTime(user)).Returns(expectedTime).IgnoreInstance();
         
        Time time = new Time();
        DateTime now = time.GetTime(user);
 
        Assert.IsTrue(now.Year == 2011);
        Assert.IsTrue(now.Month == 1);
        Assert.IsTrue(now.Day == 1);
    }


Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Sathyamurthy
Top achievements
Rank 1
answered on 21 Aug 2012, 09:45 AM
Hi Ricky,
          How can I download the version that has this fix?
          Also, your sample will not work for me. In my case, GetTime method of Time class does lot many things. what I have shown is just a scaled down version purely for the purpose of reproducing the issue.
Thanks
Sathya
0
Ricky
Telerik team
answered on 22 Aug 2012, 05:28 PM
Hi Sathya,

Thanks again for contacting us.

The build is published last week and can be downloaded from public site. However, if somehow it is now showing to you then please open a support ticket where I can send it over to you.

P.S. On August 23rd, we are doing a JustMock webinar with Chris Eargle (Just Evangelist and C# MVP), please feel free to join and share your valuable insights that might help us align the product better.

Here goes the link to signup:
https://www1.gotomeeting.com/register/210978177


Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Rayne
Top achievements
Rank 1
answered on 19 Oct 2012, 07:31 PM
This is the exact problem I'm having with the parameter of my method under test being defined in a different assembly. I know this is the issue because the test worked, until I moved the class to a new project for refactoring.
I'm using version 2012.3.1016.3 that I installed yesterday.

Is this still an issue?
0
Ricky
Telerik team
answered on 24 Oct 2012, 04:13 PM
Hi Rayne,

Thanks again for bringing up the question.

This is indeed resolved in the latest version. I am sending you a sample project that clarifies it even further.
 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Rayne
Top achievements
Rank 1
answered on 24 Oct 2012, 04:59 PM
This doesn't quite help me. Here's what I'm trying to do:

In the sample you sent, both the method (GetCurrentDate) and the parameter class (User) are defined in the same project (Core). Another thing I notice in your sample, is that you are ignoring the instance, so naturally it will work. You are bypassing the whole method call.

What I'm trying to test is that File.WriteAllBytes was called inside the method. Here is the method I'm trying to test:
public Class1 WriteBytes(FileStructure fs)
        {
            var tfs = (TextFileStructure) fs;
            tfs.Path = Path.Combine(@"C:\Temp", "hello.txt");
            try
            {
                File.WriteAllBytes(tfs.Path, tfs.Text);  
                IsSuccessful = true;
           }
            catch (FileNotFoundException)
            {
                //do nothing
            }
            return this;
        }

Here is the test class:
public void TestMethod3() {
    var called = false;
    var _sample = new Class1();
 
    // Arrange
    var fs = Mock.Create<TextFileStructure>();
    Mock.Arrange(() => fs.Text).IgnoreInstance().Returns(new byte[23]);
    Mock.Replace<string, byte[]>((s, b) => File.WriteAllBytes(s, b)).In<Class1>();
    Mock.Arrange(() => File.WriteAllBytes(null, null)).IgnoreArguments().DoInstead(() => called = true);
 
    // act
    _sample.WriteBytes(fs);
 
    // assert
    Assert.IsTrue(called);
}

The method I'm testing (WriteBytes) is a method in Class1 located in Project A. The two domain classes (TextFileStructure and FileStructure) are located in Project B. I initially had them in Project A, and the test would pass.  As soon as I moved the domain classes to Project B, the test failed, because it was actually calling File.WriteAllBytes instead of using the Mock. 

Why does it suddenly fail if I move the domain classes to their own project?
0
Ricky
Telerik team
answered on 24 Oct 2012, 09:38 PM
HI Rayne,

I updated the project with User in a different assembly. However, it is working as expected. Please check that your DLL version points to the latest one. I am also sending you the project to have a look.



Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Rayne
Top achievements
Rank 1
answered on 25 Oct 2012, 01:07 PM
I have verified that I'm using the same version of JustMock as in your sample. I created my own test of DateTime.Now and it isn't working. I'm clearly doing something wrong. I've submitted a support ticket with my sample solution. Thank you for all your help.
0
Ricky
Telerik team
answered on 25 Oct 2012, 07:47 PM
Hi Rayne,
I have sent you an updated build in ticket # 622295. Now, all your tests in the sample project pass as expected.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Rayne
Top achievements
Rank 1
answered on 25 Oct 2012, 08:40 PM
Thank you. That updated build corrected my issues.
It's good to know it wasn't my code that was the problem.
0
Kaloyan
Telerik team
answered on 26 Oct 2012, 08:09 AM
Hello Rayne,

 Thank you for reaching back to us.

 It is good to know that your issue is solved.

 Please, if there is anything else, we can assist you with, do not hesitate to ask.

Greetings,
Kaloyan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Sathyamurthy
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Sathyamurthy
Top achievements
Rank 1
Rayne
Top achievements
Rank 1
Kaloyan
Telerik team
Share this question
or