Telerik Forums
JustMock Forum
1 answer
80 views
justmock does not use

ReturnsCollection


i can,t find it .. other methods works
Ricky
Telerik team
 answered on 21 Feb 2012
3 answers
88 views
We are using the Q1 2011 release (at least until Q1 2012) is released.  I have a few problems related to objects that should be mocked and arranged not behaving as they are arranged/mocked.  We have a base class that we use for all of our testing within a class in the code, and then each method within code has a TestClass associated with it, with the seperate test methods for that method under test defined within.

here is the base class
[TestClass]
   public class SPM150ControllerTestBase
   {
 
       public IAssetsService AssetsServiceMock { get; set; }
       public INavigationService NavigationServiceMock { get; set; }
       public ISPM150Service SPM150ServiceMock { get; set; }
       public IGlobalData GlobalDataMock { get; set; }
       public SPM150Controller Target { get; set; }
 
       [TestInitialize]
       public void TestInit()
       {
           AssetsServiceMock = Mock.Create<IAssetsService>();
           SPM150ServiceMock = Mock.Create<ISPM150Service>();
           NavigationServiceMock = Mock.Create<INavigationService>();
           GlobalDataMock = Mock.Create<IGlobalData>();
           Target = Mock.Create<SPM150Controller>(Behavior.CallOriginal, new object[] { AssetsServiceMock, SPM150ServiceMock, NavigationServiceMock, GlobalDataMock, null });
 
           OnInitialize();
       }
 
       protected virtual void OnInitialize()
       { }
 
   }

now a class and method that the test is failing on....

[TestClass]
    public class UpdateEquipmentTest : SPM150ControllerTestBase
    {
        public IEquipmentListItem UpdateItemMock { get; set; }
        public IEquipmentListItem ExistingItemMock { get; set; }
 
        //[TestInitialize]
        //public void OnInit()
        //{
        //    base.TestInit();
        //}
 
        protected override void OnInitialize()
        {
            UpdateItemMock = Mock.Create<IEquipmentListItem>();
            ExistingItemMock = Mock.Create<IEquipmentListItem>();
 
            //Mock.Arrange(() => UpdateItemMock.BenchmarkValue).Returns(100);
            //Mock.Arrange(() => UpdateItemMock.Classification).Returns("No Class");
            //Mock.Arrange(() => UpdateItemMock.DeviceName).Returns("A Device");
            //Mock.Arrange(() => UpdateItemMock.IPAddress).Returns("000.000.000.000");
            //Mock.Arrange(() => UpdateItemMock.Request).Returns(0);
 
            //Mock.Arrange(() => ExistingItemMock.BenchmarkValue).Returns(100);
            //Mock.Arrange(() => ExistingItemMock.Classification).Returns("No Class");
            //Mock.Arrange(() => ExistingItemMock.DeviceName).Returns("A Device");
            //Mock.Arrange(() => ExistingItemMock.IPAddress).Returns("000.000.000.000");
            //Mock.Arrange(() => ExistingItemMock.Request).Returns(0);
 
            UpdateItemMock.BenchmarkValue = 100.0;
            UpdateItemMock.Classification = "No Class";
            UpdateItemMock.DeviceName = "A Device";
            UpdateItemMock.IPAddress = "000.000.000.000";
            UpdateItemMock.ModbusUnitId = 100;
            UpdateItemMock.Request = 0;
 
            //set with default values can change if needed...
            ExistingItemMock.BenchmarkValue = 100.0;
            ExistingItemMock.DeviceName = "A Device";
            ExistingItemMock.ModbusUnitId = 100;
            ExistingItemMock.IPAddress = "000.000.000.000";
            ExistingItemMock.Request = 0;
 
            Mock.Arrange(() => Target.LogBenchmarkValueChange(Arg.AnyString, Arg.AnyDouble, Arg.AnyString, Arg.AnyString, Arg.AnyDouble)).DoNothing();
            Mock.Arrange(() => Target.LogIPChange(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString)).DoNothing();
            Mock.Arrange(() => Target.LogModBusUnitIdChange(Arg.AnyString, Arg.AnyInt, Arg.AnyString, Arg.AnyString, Arg.AnyInt)).DoNothing();
            Mock.Arrange(() => Target.LogDeviceNameChange(Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString, Arg.AnyString)).DoNothing();
 
        }
 
 
       
        [TestMethod]
        public void Test0020CheckForSiteAndUserCalledCorrectParametersVersion1()
        {
            Mock.Arrange(() => Target.CheckForSiteNumberAndUserName(Arg.AnyString, Arg.AnyString)).DoNothing().OccursOnce();
 
            Mock.Arrange(() => SPM150ServiceMock.GetEquipmentDetails(Arg.AnyInt)).Returns(ExistingItemMock);
 
 
            Target.UpdateEquipment("12345", UpdateItemMock, "Hi Bob!");
 
            //Target.Assert();
 
            //Target.Assert(x => x.CheckForSiteNumberAndUserName("12345", "Hi Bob!"));
            Mock.Assert(() => Target.CheckForSiteNumberAndUserName("12345", "Hi Bob!"));
        }
}


with this particular example we have two issues.
1. The Call to CheckForSiteNumberAndUserName executes the original code despite the DoNothing on the arrange.
2. The call to GetEquipmentDetails within the SPM150 Mock, does nothing (as it should) but returns null despite the fact that it is arranged to return the ExistingItemMock (created in the OnInitialize method)

Any help is greatly appreciated.
Ricky
Telerik team
 answered on 17 Feb 2012
1 answer
176 views
Hi,

I have just started using the evaluation version of JustMock. I am trying to run my tests through NUnit in an environment that does not have Visual Studio installed (a build server with .NET 4.0 redist installed). It appears to work for all tests that do not require mocking static methods, however when I attempt to run a test with a mocked static method I get the following error:

MyTest.TestConnect:
Telerik.JustMock.MockException : Profiler must be enabled to mock/assert target MyClass.AStaticMethod(Boolean) method.

When I look up your documentation at this link it only gives information on how to configure the advanced features when using Visual Studio.

Do you guys support using NUnit GUI as the test runner?
Is it possible that there is an issue with the fact that I have not installed JustMock on my build server (I'm just copying the Telerik.JustMock.dll at the moment)?
I am currently building in Release mode as the .NET 4.0 debug dlls are not present on my build server - could this be an issue?

If there is no way to get this working I can just wrap the static methods with a dependency injected adaptor interface, but it would be useful for me to be able to avoid introducing the extra class.

Thanks.
Ricky
Telerik team
 answered on 17 Feb 2012
1 answer
225 views
In a nutshell I am receiving the following error.
Test method SPM150.Test.LogBenchmarkValueChangeTest.Test0020LogTransactionCalledWhenValuesDifferentCorrectParmetersVersion1 threw exception:
Telerik.JustMock.MockException: Profiler must be enabled to mock/assert target SPM150ControllerTestBase.get_AssetsServiceMock() method.
at Telerik.JustMock.Handlers.InterceptorHandler.Create(Object target, MethodInfo methodInfo, Boolean privateMethod)
at Telerik.JustMock.MockContext`1.SetupMock(MockExpression`1 expression)
at Telerik.JustMock.Mock.<>c__DisplayClass13.<Assert>b__12(MockContext`1 x)
at Telerik.JustMock.MockContext.Setup(Instruction instruction, Action`1 action)
at Telerik.JustMock.Mock.Assert(Expression`1 expression, Args args, Occurs occurs)
at Telerik.JustMock.Mock.Assert(Expression`1 expression)
at SPM150.Test.LogBenchmarkValueChangeTest.Test0020LogTransactionCalledWhenValuesDifferentCorrectParmetersVersion1() in SPM150ControllerTest.cs: line 545

I am running my unit test using the Resharper 6.1 Test Runner in Visual Studio 2010.  I recently upgraded from the Q1 2011 Free edition to the Q3 2011 Free edition.  before the upgrade all of the tests ran without any exception.

I upgraded because I was having an issue where a test was passing despite the fact that I changed a value that should have made it fail, and though by chance it was something that could be fixed by doing an upgrade.

Any help you can provide is greatly appreciated.

Thanks.
Ricky
Telerik team
 answered on 14 Feb 2012
3 answers
91 views
Just looking for some example on how to correctly Mock the ORM entities model.
any help would be great

Thanks
Andrew
Top achievements
Rank 1
Veteran
Iron
 answered on 09 Feb 2012
3 answers
152 views
Hi all,

I have the following class (simplified)

public abstract class AbstractStateMachine
{
      public void DoClose()
      {
        //do something
         close();
      }
      public abstract void close();
}

I want to test if the close() method is called, so I thought I could do the following:
[TestMethod]
public void TestMethod1()
{
    AbstractStateMachine sm = Mock.Create<AbstractStateMachine>(Behavior.CallOriginal);
    Mock.Arrange(() => sm.close()).DoNothing().MustBeCalled();
    sm.DoClose();
}

As a result, I get a AccessViolationException in the sm.DoClose() method.
What am I doing wrong?

Ricky
Telerik team
 answered on 26 Jan 2012
1 answer
47 views
I found an issue with Long's and Int's being treated as different objects in the Mocks.

Here's my test class:

public class MyClass
{
    public long AddOne(long number)
    {
        return number + 1;
    }
}

Here's my test that fails:

[TestMethod]
public void MyClass_AddOne_mock_test_int()
{
    // Arrange
    int number = 5;
    int expectedResult = 42;
 
    var myClass = Mock.Create<MyClass>();
 
    Mock.Arrange(() => myClass.AddOne(number))
        .Returns(expectedResult);
 
    // Act
    var result = myClass.AddOne(number);
 
    // Assert
    Assert.AreEqual(expectedResult, result); // result = 0
}

If I change "number" to a long the test passes.

This also holds true for this:

Mock.Assert(() => myClass.AddOne(number), Occurs.Once());


My expectations were for int and long to produce the same result since they are "equal" and there is no way to actually pass a true int into that method.

[TestMethod]
public void Assert_int_vs_long()
{
    int intNumber = 5;
    long longNumber = 5;
 
    Assert.AreEqual(intNumber, longNumber);
}


Ricky
Telerik team
 answered on 19 Jan 2012
4 answers
129 views
Ok, this one took me a WHILE to figure out.  Not sure exactly how to explain it...

but this scenario will fail to be mocked:
public static bool DirectoryExists(string directory)
{
    return DirectoryExists(new DirectoryInfo(directory));
}
 
private static bool DirectoryExists(DirectoryInfo directory)
{
    bool value = Directory.Exists(directory.FullName);
    return value;
}

My test calls the first method, which then calls the 2nd method.

The Directory.Exists will NOT be mocked if the method is marked as private.  If I change it to public, the mock will take place.

I've included a sample project for your review.

http://dl.dropbox.com/u/1006254/JustMockTestProject4.zip

forgot to mention: I'm using MSTest
Joel
Top achievements
Rank 1
 answered on 10 Jan 2012
1 answer
67 views

I have references to arrays inside my Mock.Assert's expression statement which is causing the test to fail.

Here's my test that fails:
[TestMethod]
public void JustMockTest_String_Arrays()
{
    // Arrange
    Mock.SetupStatic(typeof(System.IO.File));
 
    string[] sourceFiles = new string[] { @"X:\source\file1.txt" };
    string[] destinationFiles = new string[] { @"X:\destination\file1.txt" };
 
    // Act
    System.IO.File.Copy(sourceFiles[0], destinationFiles[0]);
 
    // Assert
    Mock.Assert(() => System.IO.File.Copy(sourceFiles[0], destinationFiles[0]), Occurs.Once());
}

Though I noticed if I call the ToString() method, I can get the test to pass:
[TestMethod]
public void JustMockTest_String_Arrays_ToString()
{
    // Arrange
    Mock.SetupStatic(typeof(System.IO.File));
 
    string[] sourceFiles = new string[] { @"X:\source\file1.txt" };
    string[] destinationFiles = new string[] { @"X:\destination\file1.txt" };
 
    // Act
    System.IO.File.Copy(sourceFiles[0], destinationFiles[0]);
 
    // Assert
    Mock.Assert(() => System.IO.File.Copy(sourceFiles[0].ToString(), destinationFiles[0].ToString()), Occurs.Once());
}

Ricky
Telerik team
 answered on 10 Jan 2012
2 answers
116 views
Mocking the DTE and child properties
Below I have created mocks for my dte objects so that I can test some extension methods:

var Dte = Mock.Create<DTE>();
var activeProject = Mock.Create<Project>();
var solution = Mock.Create<Solution>();
Mock.SetupStatic(typeof(RazorTemplateExtensions));

Mock.Arrange(() => Dte.Solution).Returns(solution);

Mock.Arrange(() => RazorTemplateExtensions.Dte).Returns(Dte);
Mock.Arrange(() => Solution.FileName).Returns("SolutionNameTest");
Mock.Arrange(() => ActiveProject.RootNamespace()).Returns("DefaultNamespace");
Mock.Arrange(() => Solution.ActiveProject()).Returns(ActiveProject);

The italicized arranges above work as I expect them to but when it comes time for testing the Solution object in the extensions class the solution instance is always assigned to null. If I stop in the immediate window and type Dte.Solution, I can see the proxy object as expected. But, when the solution variable is set in the below code the it is always set to null.
 

public static String DirectoryPath(this RazorTemplate razorTemplate)
{
    var solution = Dte.Solution;
     // while breaking here this solution is null
     // In the immediate window the Dte.Solution returns proxy object.
     ...
}

The only other clues that I have found are that I get this error when I try to access the ActiveProject() from the proxy object:

'EnvDTE.Solution' does not contain a definition for 'ActiveProject' and the best extension method overload 'Ellevate.T4Razor.Model.VisualStudioExtensions.SolutionExtensions.ActiveProject(EnvDTE.Solution)' has some invalid arguments

Ricky
Telerik team
 answered on 04 Jan 2012
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?