
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
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.

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.
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.

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);
}
}
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.

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
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
Ricky
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

I'm using version 2012.3.1016.3 that I installed yesterday.
Is this still an issue?
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.
Ricky
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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?
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.

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.

It's good to know it wasn't my code that was the problem.
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.