Telerik Forums
JustMock Forum
5 answers
142 views

Let's say I have a class

 

class MyClass
{
    public virtual void MyFunc1( DateTime somedate)
    {}

    public virtual void MyFunc2( )
    {
       MyFunc1(SomeObject.SentDate)

    }

}

where SomeObject has a field called SentDate.

SomeObject is in a global scope and, for some reasons, I can't provide a value for it so will always be null when mocking.

I can arrange for MyFunc1() to do whatever I want but, when I call MyFunc2(), it will try to evaluate its parameter SomeOject.SentDate and, because SomeObject is null, this will fail with an exception.

Is there a way to skip the evaluation of SomeObject.SentDate parameter?

Stefan
Telerik team
 answered on 12 Aug 2015
1 answer
110 views

I have a private Generic method and I want to call it using Private Accessor, How can i do that.

private void PopulateCollection<T>(List<IDataItem> fromCollection, ObservableCollection<T> toCollection){}

 

Thanks

Vikas

Stefan
Telerik team
 answered on 11 Aug 2015
5 answers
129 views

 I have this code:

<p>using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;
using Telerik.JustMock.Core;
  
  namespace TestMock1
{
    [TestClass] public class UnitTest1 {
         
[TestMethod]
        public void TestMethod1()
        { var mock = Mock.Create<Cls1>(); Mock.Arrange(() => mock.Func1()).Returns(2);
              mock.Func2();
        }
    }
  public class Cls1 { public Cls1()
        {
  
        }
  internal virtual int Func1()
        { int x = 1; return x;
        }
  internal virtual int Func2()
        { return Func1();
        }
  
    }
  
  
}​</p>

Florian
Top achievements
Rank 1
 answered on 07 Aug 2015
1 answer
75 views

Hello,

I am trying to do Mock.Arrange() in the following manner:

 var practice = Mock.Create<PracticeOptions>();
 Mock.Arrange(() => practice.PolicyExpirationDays).Returns(30);

I am getting the exception in the second line saying that "Cannot mock 'Int32 get_PolicyExpirationDays()'. The profiler must be enabled to mock, arrange or execute the specified target."

I tried updating\removing\installing JustMock on my machine but nothing is working out.

Please look into this asap as it's a bit urgent.

Thanks,

Abdeali

Kaloyan
Telerik team
 answered on 06 Aug 2015
6 answers
465 views

If given the following code, is it possible to mock the Base class constructor without mocking the derived class constructor so that MyProp gets set?:

    public class Base
    {
        public Base()
        {
            throw new NotImplementedException();
        }

        public string MyProp { get; set; }
    }

    public class Derived : Base
    {
        public Derived()
        {
            MyProp = "hello";
        }
    }

Stefan
Telerik team
 answered on 21 Jul 2015
1 answer
156 views

My team has been using Microsoft Fakes for a while, but recently looked into JustMock and really liked what we saw and wanted to use JustMock going forward and slowly replace all MS Fakes uses to JustMock.  After a bit of hassle getting our Build Server to handle JustMock appropriately, it seems that none of our tests that used MS Fakes now work, getting the error: Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to get address of function SetDetourProvider from library 'C:\Program Files (x86)\Telerik\JustMock\Libraries\CodeWeaver\32\Telerik.CodeWeaver.Profiler.dll'.

This is a big problem for us as we have too many tests using Fakes to convert them all at once and can't afford an all or nothing approach here.  Is there a way to get them both working on the same Build Server?  Each solution/build only uses one framework...

Thanks!

Stefan
Telerik team
 answered on 21 Jul 2015
3 answers
374 views

I'm trying to understand if JustMock and Microsoft Fakes both have a place in the same project as complementary tools, or if it's a case of choosing one versus the other.

thanks

Stefan
Telerik team
 answered on 21 Jul 2015
3 answers
127 views

Hi, I am testing a method whereby a value is set in the method. Initially I set the return value using JustMock. This is SharePoint by the way.

01.// arrange
02.SPItemEventProperties properties = Mock.Create<SPItemEventProperties>(Behavior.Strict);
03.Mock.Arrange(() => properties.BeforeProperties["Title"]).Returns("Old Title");
04.Mock.Arrange(() => properties.AfterProperties["Title"]).Returns("New Title");
05. 
06.// act
07.Person p = new Person();
08.p.DoSomething(properties);     // -> properties.AfterProperties["Title"] = "abc";
09. 
10.// assert
11.Assert.AreEqual("abc", properties.AfterProperties["Title"].ToString()); // fails here since AfterProperties is still "New Title"

I realize that the .Return is causing the same value to be returned. How can I check that the correct value is being set inside the method / can I get the value which has been set in the method back in the assert.arequal?

 

Thanks,

Gary.

 

Stefan
Telerik team
 answered on 30 Jun 2015
13 answers
131 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
102 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
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?