Telerik Forums
JustMock Forum
13 answers
143 views

We are trying to write an Add() and Delete() test with JustMock against the Telerik ORM our gets and updates work fine but not sure what we are missing as we cannot get Adds and Deletes to work.

 

This is what we have:

[TestInitialize]
[Description("Test Initialize")]
public void TestInitialize()
{
    emIRIS = Mock.Create<emIRIS>();
 
    Mock.Arrange(() => emIRIS.CourseSections).ReturnsCollection(GetCourseSectionData());
    Mock.Arrange(() => emIRIS.FacultyCourseSections).ReturnsCollection(GetFacultyCourseSectionData());
}
 
[TestCleanup]
public void TestCleanup()
{
    emIRIS = null;
}
 
[TestMethod]
[Description("test")]
public void VFaculty_Test_Test()
{
    var preResults = emIRIS.FacultyCourseSections.Where(x => x.CourseSection_pk == 20).ToList();
 
    emIRIS.FacultyCourseSections.Count().Should().Be(6);
 
    emIRIS.Delete(preResults[0]);
 
    emIRIS.SaveChanges();
 
    emIRIS.FacultyCourseSections.Count().Should().Be(5);
 
}

 In the above test method we want to delete an item from the collection but nothing happens and the last assert test will fail.

Do we need to Mock the Delete Method on emIRIS to make the delete work? if so how.

And for adding new objects to the collections do we need to Moch the Add method on each collection?

Any Ideas???

 

Thanks

 

 

 

 

 

Kaloyan
Telerik team
 answered on 24 Jun 2015
3 answers
109 views

Currently we are using JustMock Lite with xUnit 1.9.2 and have test cases where we create reusable mocks for our test methods within the controller of a test class. My team bumped into an issue after performing an upgrade to version 2.0. It appears when using the constructor to mock a method from a class (for reuse in test methods) and then creating a second mocked function within the test method (which is a different method from the same class that was defined in the constructor) causes the mocked implementation of the method in the constructor to disappear.

Please see the test example below. In the test class MockIssueExample, I have test methods that verifies the return value from an interface method or virtual function. In version 1.9.2, the mocks defined in SetupMocks() returns the value of 5 within each of the test methods. When I upgrade to version 2.0, each of the mocked methods returns a value of 0 in the test method and causes my test to fail.

Note: In each of the test methods I left a line of code that fixes the issue but it requires me to copy the mocked implementation from the constructor into the test method.Has something changed in 2.0 that's causing this scoping issue? Can we mock one function within a constructor and a second method within the actual test method without losing the mock implementation that what was defined in the constructor.

Please Advise. Thanks!

 public class MockIssueExample
{
private ITestMethods _testMethods;
private ClassUnderTest _classUnderTest;
private ClassTestMethods _classTestMethods;

public MockIssueExample()
{
SetupMocks();

_classUnderTest = new ClassUnderTest(_testMethods, _classTestMethods);
}

private void SetupMocks()
{
_testMethods = Mock.Create<ITestMethods>();
_classTestMethods = Mock.Create<ClassTestMethods>();

Mock.Arrange(() => _testMethods.FunctionReturnsInteger_1()).Returns(5);

Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_1()).Returns(5);
}

[Fact]
public void Virtual_function_1_should_return_the_mocked_value()
{
//Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_1()).Returns(5);
Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_2()).Returns(6);

int returnVal = _classUnderTest.GetValueReturnedFromVirtualFunction_1();

Assert.True(returnVal == 5);
}

[Fact]
public void Interface_function_1_should_return_the_mocked_value()
{
//Mock.Arrange(() => _testMethods.FunctionReturnsInteger_1()).Returns(5);
Mock.Arrange(() => _testMethods.FunctionReturnsInteger_2()).Returns(9);

int returnVal = _classUnderTest.GetValueReturnedFromInterfaceFunction_1();

Assert.True(returnVal == 5);
}
}

 

public interface ITestMethods
{
int FunctionReturnsInteger_1();
int FunctionReturnsInteger_2();
}

 

public class ClassTestMethods
{
public virtual int VirtualFunctionReturnsInteger_1()
{
return 1;
}

public virtual int VirtualFunctionReturnsInteger_2()
{
return 2;
}
}

public class ClassUnderTest
{
private readonly ITestMethods _testMethods;
private readonly ClassTestMethods _classTestMethods;

public ClassUnderTest(ITestMethods testMethods, ClassTestMethods classTestMethods)
{
_testMethods = testMethods;
_classTestMethods = classTestMethods;
}

public int GetValueReturnedFromVirtualFunction_1()
{
return _classTestMethods.VirtualFunctionReturnsInteger_1();
}

public int GetValueReturnedFromInterfaceFunction_1()
{
return _testMethods.FunctionReturnsInteger_1();
}
}

Stefan
Telerik team
 answered on 17 Jun 2015
3 answers
159 views

Like the user in this thread I'm having a problem getting my unit tests running in TFS. In my case, I'm running TFS 2012. However, having read through the thread I assumed that the process to fix it would be the same.

A little more reading and I found this documentation article and I followed the steps shown there. In my case I followed the "Steps for integrating JustMock in updated build configurations (DefaultTemplate.11.1.xaml)".

I set up the Build Controller to get the required assemblies (jm01.png)

I modified the build template (jm02.png)

After checking in the modified template I ran my build. It failed.

"Test method Genesis.Service.Implementation.Tests.Digests.WeeklyDigestFixture.ShouldCallAllSections threw exception: 
Telerik.JustMock.Core.ElevatedMockingException: Cannot mock 'Int32 get_DigestsToBeIncludedOn()'. The profiler must be enabled to mock, arrange or execute the specified target.
"

 Thinking I must have missed a step and that the profiler wasn't working I investigated further.

jm03.png and jm04.png, however, suggest that I didn't.

So why are my tests failing?

Stuart Hemming
Top achievements
Rank 2
 answered on 05 Jun 2015
1 answer
166 views

In a test method, I'm trying to mock an AutoMapper call and return the object I created in my test method:

 Mock.Arrange(() => _mappingEngine.Map<DtcIgnoreModel, DTCIgnore>(dtcIgnoreMockObj, Arg.IsAny<DTCIgnore>())).Returns(dtcIgnore);

But I get an IndexOutOfRangeException when I hit this line. Says it "occurred in Telerik.JustMock.dll but was not handled in the user code. Additional Information: Index was outside the bounds of the array."

I'm not sure what array it's referring to. Any advice would be appreciated.

 

-L

Kaloyan
Telerik team
 answered on 03 Jun 2015
3 answers
127 views

Hi.

My configuration: Visual Studio 2015 preview version + JustMock Q2 (2015.2.512.4)

Apparently static mocking doesn't work for me when i run it from Windows Universal unit test project. It seems ignoring the mock at all. For example here it doesn't throw exception:

Mock.SetupStatic(typeof(Assert), Behavior.Strict);
Mock.Arrange(() => Assert.IsTrue(Arg.AnyBool)).Throws(new ArgumentException());
Assert.IsTrue(true);

The same code executes correctly if i create regular unit test project on the same configuration.

 Any help?

Thanks.

Kaloyan
Telerik team
 answered on 25 May 2015
1 answer
158 views

I'm trying to make a mock that verifies that none of the Error or ErrorFormat calls on the log4net ILog interface were called. Given the number of various signatures this seems a bit annoying. Any better ideas that literally making a match for all 7? The .IgnoreArguments() isn't that helpful given I need to match each signature first before ignoring and putting all nulls causes ambiguous call errors so I have to use Arg.IsAny<> all over.

Signatures I want matched:
void Error(object message);

void Error(object message, Exception exception);

void ErrorFormat(string format, params object[] args);

void ErrorFormat(string format, object arg0);

void ErrorFormat(string format, object arg0, object arg1);

void ErrorFormat(string format, object arg0, object arg1, object arg2);

void ErrorFormat(IFormatProvider provider, string format, params object[] args);

Example of matching:

            Mock.Arrange(() => _Logger.Error(Arg.IsAny<object>())).OccursNever();
            Mock.Arrange(() => _Logger.Error(Arg.IsAny<object>(), Arg.IsAny<Exception>())).OccursNever();
... do I have to do all 7 this way?

Stefan
Telerik team
 answered on 19 May 2015
2 answers
159 views

@SET JUSTMOCK_INSTANCE=1
@SET COR_ENABLE_PROFILING=1
@SET COR_PROFILER={B7ABE522-A68F-44F2-925B-81E7488E9EC0}

When I run XUNIT, this works fine. : 

xunit.console %TestsDLL% -nunit %ReportDirectory%\%CurrentProjectRunning%.%ldt%.xml

Results: 

.NET Runtime version 4.0.30319.0 - The profiler was loaded successfully.  Profiler CLSID: '{B7ABE522-A68F-44F2-925B-81E7488E9EC0}'.  

 No error the profiler seems to work. 

 

When I run XUNIT from OpenCover it seems to lose these settings?

 OpenCover.console.exe -target:"c:\xunitrunner\xunit.console.exe" -mergebyhash -filter:"+[*]*" -targetargs:"%TestsDLL% -nunit %ReportDirectory%\%CurrentProjectRunning%.%ldt%.xml"  -output:%ReportDirectory%\%ldt%coverage.xml -register:user

Results:

.NET Runtime version 4.0.30319.0 - The profiler was loaded successfully.  Profiler CLSID: '{1542C21D-80C3-45E6-A56C-A9C1E4BEB7B8}'.  

With this error:
The profiler must be enabled to mock, arrange or execute the specified target.
Detected active third-party profilers:
* OpenCover (from process environment)
Disable the profilers or link them from the JustMock configuration utility. Rest
art the test runner and, if necessary, Visual Studio after linking.

 

Kaloyan
Telerik team
 answered on 18 May 2015
0 answers
76 views

Hi.

My configuration is: Visual Studio 2015 preview + JustMock Q2 2015 (2015.2.512.4)

Apparently static mocks don't work for me when i execute them from Windows Universal unit test project. It seems like the mock is not applied at all and the actual code is executed. For example here exception is not thrown:

Mock.SetupStatic(typeof(Assert), Behavior.Strict);
Mock.Arrange(() => Assert.IsTrue(Arg.AnyBool)).Throws(new ArgumentException());
Assert.IsTrue(true);

The same code though executes correctly and exception is thrown when i execute it from the regular unit test project.

Thank you.

 

Anton
Top achievements
Rank 1
 asked on 15 May 2015
3 answers
227 views

Hi Telerik team,

 

Is it possible to use Mock.Arrange to mock a C++/CLI function that has parameters which are pointers to native C++ structs? Function prototype would be similar to the following:

int foo(NativeStruct1* param1, NativeStruct2* param2)

 

Best Regards,

David

 

Stefan
Telerik team
 answered on 15 May 2015
2 answers
601 views

I've found a couple of articles about testing protected methods and have written the appropriate code to do so.

However, now I want to test my class, which includes a public abstract method, to ensure, when the abstract method is called in this particular implementation, that one or more protected methods are called. Something like this ...

01.public abstract class MyBaseClass
02.{
03.    protected void Foo(){ /* stuff */ }
04.    protected void Bar(){ /* stuff */ }
05.     
06.    public abstract void Stuff();
07.}
08. 
09.public class MyClass : MyBaseClass
10.{
11.    public override void Stuff()
12.    {
13.        Bar();
14.    }
15.}

 

In this trivial example I want my test to ensure that Bar() is called.

 Can I actually do this?

Stefan
Telerik team
 answered on 12 May 2015
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?