Telerik Forums
JustMock Forum
1 answer
90 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
126 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
75 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
130 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
264 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
105 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
278 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
400 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
3 answers
125 views
Hi!

Please consider the case when the method I want to test looks like this:

public class Foo {
    public void TestMe(<arg list>)
    {
        [business logic omitted...]

         Helper hlp = new Helper();
         bool wasHelped = hlp.HelpMe(<arg list>);   

        [business logic omitted...]
    }
}

Since the object "hlp" is initialized within the method that's to be tested, as opposed to being instantiated by the test method, will JustMock allow mocking the (non-virtual, non-static) method "Helper.HelpMe" such that the above call to "HelpMe" is re-routed to the mocked implementation? If so, what would be the syntax for doing this?

Ricky
Telerik team
 answered on 21 Nov 2011
3 answers
128 views
The following test fails:
namespace JustMockExamples
{
    class UsedClass
    {
        public int ReturnFive()
        { return 5; }
    }
 
    [TestClass]
    public class InstancesBug
    {
        [TestMethod]
        public void SimpleFake_ControlAllInstances()
        {
            var fakeUsed = Mock.Create<UsedClass>();
            Mock.Arrange(() => fakeUsed.ReturnFive()).Returns(7);
 
            Assert.AreEqual(7, fakeUsed.ReturnFive());
 
            var realInstance = new UsedClass();
            Assert.AreEqual(5, realInstance.ReturnFive());
        }
    }
}

Any idea why?
Ricky
Telerik team
 answered on 09 Nov 2011
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?