Telerik Forums
JustMock Forum
1 answer
72 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
121 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
1 answer
96 views
I setup two tests, one with MustBeCalled() and one with OccursNever(), but they both pass.

Here's my tests...

[TestMethod]
public void MyClass_GetInt_with_MustBeCalled()
{
    // Arrange
    var myClass = Mock.Create<MyClass>();
 
    Mock.Arrange(() => myClass.GetInt())
        .Returns(911)
        .MustBeCalled();
 
    // Act
    int result = myClass.GetInt();
 
    // Assert
    Assert.AreEqual(911, result);
    Mock.Assert(myClass);
}
 
[TestMethod]
public void MyClass_GetInt_with_OccursNever()
{
    // Arrange
    var myClass = Mock.Create<MyClass>();
 
    Mock.Arrange(() => myClass.GetInt())
        .Returns(911)
        .OccursNever();
 
    // Act
    int result = myClass.GetInt();
 
    // Assert
    Assert.AreEqual(911, result);
    Mock.Assert(myClass);
}


I uploaded a sample project here: http://dl.dropbox.com/u/1006254/JustMockTestProject2.zip
Ricky
Telerik team
 answered on 03 Jan 2012
4 answers
133 views
I'm using MS Test, VS.NET 2010, I have JustMock Enabled and I'm mocking an ADO.NET SQL Entity.

Following the directions here:  http://www.telerik.com/help/justmock/basic-usage-entity-framework-mocking.html


[TestMethod]
public void EntityTest()
{
    // Arrange
    var siteFeatures = new List<SiteFeature>();
 
    var entities = Mock.Create<MyEntities>();
 
    Mock.Arrange(() => entities.SiteFeatures)
        .ReturnsCollection(siteFeatures)
        .MustBeCalled();
 
    // Act
    var exists = entities.SiteFeatures.FirstOrDefault(s => s.SiteId == 123) != null;
 
    // Assert
    Assert.IsFalse(exists);
}


I'm getting this error:

Test method EntityTest threw exception: 
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)


Am I doing something wrong?  Is this an issue with MS Test?
Ricky
Telerik team
 answered on 30 Dec 2011
1 answer
82 views
When I run the R# Test runner inside of VS 2010 on the individual test methods while using JustMock the tests run as expected. While in the same test session I run all tests for the given class all my Mock arranges I have confirmed get called however the delegates inside never get executed.  I am sure something is running in a seperate process somewhere but seems to be a bit odd that we hit the arrange code but the mocks never take over and my actual code that I am trying to mock executes. Again this only happens when I select run all tests for the class. Is this a R# issue or a JustMock issue? Any feedback would be appreciated. I have confirmed the Mock.Arranges are not throwing any exceptions and the R# test runner runs from the project output folder where the just mock assemblies are located at. 

One last thing to add when I run the Visual Studio Test Runner all the tests pass successfully.

Thanks,
Jake
Ricky
Telerik team
 answered on 23 Dec 2011
3 answers
140 views
Hi,

Is it possible to run the commercial version of JustMock together with NCover for sealed classes?

If so, how is this done? When we try to run them together the unit tests all fail because it can't create the mocked objects


Craig
Ricky
Telerik team
 answered on 22 Dec 2011
3 answers
286 views
Hello,

I'm trying to use SetupStatic or Initialize on AutoMapper.Mapper class (http://automapper.org/) and JustMock fails to setup the static class.
You can download the very simple test at http://www.2shared.com/file/tECy5No-/MockInitializeAutoMapperTest.html

Thanks for your help.

Ricky
Telerik team
 answered on 20 Dec 2011
1 answer
110 views
Hello together

I'm just trying to mock the SPContentType class out of the SharePoint assembly but I'm facing several problems because of its nested constructors. I've already searched for a solution but cannot find any information about the mocking of this class.

When simply trying to mock this class:

SPContentType mockedContentType = Mock.Create<SPContentType>();

I receive the following error:

Initialization method X.KeywordFinderTests.Initialize threw exception. System.MissingMethodException: System.MissingMethodException: Constructor on type 'Microsoft.SharePoint.SPContentType' not found..

After that I had a look at the SPContentType class using dotPeek; but it has an internal empty constructor. Well it has many internal calls so I tried the following code to avoid them:

SPContentType mockedContentType = Mock.Create<SPContentType>(SPContentTypeId.Empty);
 
SPContentType mockedContentType = Mock.Create<SPContentType>(new SPContentTypeId("0x01"));

SPContentType mockedContentType = Mock.Create<SPContentType>(Constructor.Mocked);

But those constructor calls fail with the same error message. Even trying to create a mock using any of the other public constructor always leads to an error message.

SPContentType mockedContentType = new SPContentType(new ContentTypeId("0x01"), mockedContentTypeCollection, "My Content Type");
Initialization method X.KeywordFinderTests.Initialize threw exception. System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object..

Can some one help me to mock this class?

Thanks,
Andreas
Ricky
Telerik team
 answered on 20 Dec 2011
21 answers
283 views
Can I get a real world example on using this product?

(in asp.net)

Does this work with WebAii somehow?
Ricky
Telerik team
 answered on 19 Dec 2011
1 answer
415 views
Trying to mock a class provided by a vendor that contains a number of public fields.

Either I'm not arranging the mock properly or JustMock does not support mocking fields. 

Sample code

// sample class to mock
public class HasFields
{
      public string FieldA;
}

// code to set up mokc
    var mock = Mock.Create<HasFields>();
    Mock.Arrange (() => mock.FieldA).Returns("it works");

When executed the Mock.Arrange call throws the error:

Telerik.JustMock.MockAssertionException: Lambda must contain a valid method to procceed

In the test code if the field is changed to a property, the Mock.Arrange works. 

Unfortunately changing the field to a property isn't an option with the classes provided by a vendor.
Ricky
Telerik team
 answered on 28 Nov 2011
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?