Telerik Forums
JustMock Forum
2 answers
108 views
Hi there,

So I have been looking at some just mock examples but can't put together how we would verify that calling a particular method did something, i.e. called another method. Here is the snippet I want to test.
public void Initialize()
   {
     //Initialize the vendor mapping module
     container.RegisterInstance<VendorMappingViewModel>(new VendorMappingViewModel(eventAggregator));

I want to make sure that contianer.RegisterInstance is called when Initialize is called. I can't test a variable cause it isn't like it stores something somewhere... Is there an example of this somewhere?

Thanks!!!
Ricky
Telerik team
 answered on 30 May 2011
3 answers
154 views
I am attempting to use JustMocks to unit test our BLL and DAL layer classes, but I am having trouble getting it to work. I suspect that the issue is that I am not setting up the tests properly for the code. Here is a simple example that follows the pattern of the code under test.

First the object
//BO
public class Person
{
    public int ID {get; set;}
    public string name {get; set;}
    public int age {get; set;}
    public bool isMale {get; set;}
}

Then the DAL
//DAL
public class PersonDbo
{
    public static BO.Person GetPersonByID(int ID)
    {
        BO.Person result = new BO.Person();
         
        //Helper method our project uses
        Database db = Helper.CreateDbConnection();
         
        using (DbCommand dbCommand = db.GetStoredProcCommand("dbo.Select_Person_ByID"))
        {
            db.AddInParameter(dbCommand, "@PersonID", DbType.Int32, ID);
             
            using (IDataReader r = db.ExecuteReader(dbCommand))
            {
                if (r.Read())
                {
                    result.ID = r.GetInt32(r.GetOrdinal("PersonID"));
                    result.name = r.GetString(r.GetOrdinal("Name"));
                    result.age = r.GetInt32(r.GetOrdinal("Age"));
                    result.isMale = r.GetBoolean(r.GetOrdinal("Is_Male"));
                }
                else
                    return null;
                return result;
            }
        }
    }
}
 
Then the BLL
//BLL
public class PersonManager
{
    public static BO.Person GetPerson(int ID)
    {
        BO.Person result = PersonDbo.GetPersonByID(ID);
     
        return result;
    }
}

And finally the test
//Test
[TestClass()]
public class PersonManagerTest
{
    [TestMethod()]
    public void GetPersonTest()
    //Arrange
    Person tom = new Person() { ID = 7, name = "Tom", age = 34, isMale = true};
    Mock.SetupStatic<PersonManager>(Behavior.Strict);
    Mock.Arrange(() => PersonManager.GetPerson(7)).Returns(tom).MustBeCalled();
     
    //Act + Assert
    Assert.AreEqual(PersonManager.GetPerson(7), tom);
}

Am I doing these right?
Ricky
Telerik team
 answered on 27 May 2011
2 answers
133 views
Hello,

I've started to play around with JustMock and so far I really like the API and the features. I've found one issue so far that's preventing me from switching over to JustMock, though. ArrangeSet with MustBeCalled does not appear to be working. Here's a code sample:

[TestClass] 
public class Test 
    [TestMethod] 
    public void TestFoo() 
    { 
        var mockFoo = Mock.Create<Foo>(); 
        Mock.ArrangeSet(() => mockFoo.A = 1).MustBeCalled(); 
        //mockFoo.A = 2; 
        Mock.Assert(mockFoo); 
    } 
 
public class Foo 
    public int A { getset; } 

The TestFoo test passes even though the mockFoo.A set accessor was not called. If I uncomment the mockFoo.A = 2 line, it still succeeds even though the value (2) doesn't match what was specified in the ArrangeSet Action (1).

Is this a known issue or am I missing something? This test was run in .NET 4.0 under VS2010.

Thanks,
Jack L
Jürgen
Top achievements
Rank 1
 answered on 26 May 2011
3 answers
145 views
Hi @All,

I'm new using JustMock. It seems to be very different to other Frameworks like RhinoMock ;-)

I want to test the gathering of parameters passed via QueryString in the Url of the Silverlight hosting page. I have to use HtmlPage.Document.QueryString to do this, but how can I mock this?

I need to mock HtmlPage.Document.QueryString
I need to return a custom Dictionary<string, string>

I couldn't found anything in the documentation about that
Jürgen
Top achievements
Rank 1
 answered on 26 May 2011
6 answers
146 views
Hi,

I've got the following test:

        [TestMethod]
        public void PayrollViewModel_CallConstructor_DisplayNameSet()
        {
            // Arrange
            var context = Mock.Create<PersonnelDomainContext>();
            var session = Mock.Create<ISession>();
            var eventAgg = Mock.Create<IEventAggregator>();
            var viewModel = new PayrollViewModel(context, session, eventAgg);
 
            // Act
            viewModel.GetPersonnel();
 
            // Assert
            Assert.AreEqual("Personnel List", viewModel.DisplayName"Display name is incorrect");
        }

Basically, I'm trying to Mock the DomainContext that is generated by RIA Services. I'd like all the service calls to the services on the server side to be mocked so they are not run/called.
I'm getting the following error:

TestMethod: PayrollViewModel_CallConstructor_DisplayNameSet
TestClass: PayrollViewModelTests
Result: Failed

Exception:
Could not create a mock from the specified target.
   at Telerik.JustMock.MockManager.CreateInstance(Type target, Container container, Boolean profilerEnabled)
   at Telerik.JustMock.MockManager.SetupMock(Type target, Behavior behavior, Boolean static)
   at Telerik.JustMock.MockManager.CreateInstance()
   at Telerik.JustMock.Mock.Create(Type target, Behavior behavior, Object[] args)
   at Telerik.JustMock.Mock.Create(Type target, Object[] args)
   at Telerik.JustMock.Mock.Create[T]()
   at PayrollPrototype.Client.Personnel.Tests.PayrollViewModelTests.PayrollViewModel_CallConstructor_DisplayNameSet()

Failed

I suspect it is because the DomainContext is sealed. So how do I mock it, I'm supposed to be able to mock just about anything with justmock. (pardon the pun). I've been reading about mocking for some time but am still weak in the implementation. Need more practice. I keep seeming to hit errors when I try to mock anything.

thanks,
Stephen

Ricky
Telerik team
 answered on 20 May 2011
2 answers
124 views
Hi,

I am new to mocking and started looking at JustMock. But I can't seem to figure out how I can verify the order of method calls.

For an example to assert that OnStart() was called exactly once before OnStop() was called exactly once on my mock object.

Is this possible?

Sincerely, Peter
Peter
Top achievements
Rank 1
 answered on 19 May 2011
2 answers
135 views
I am running into some exceptions when I try and mock some property calls from an entity framework model.

var fakeEntities = Mock.Create<SomeEntities>(Constructor.Mocked);
  
Mock.Arrange(() => fakeEntities.SomeObjectSet.Count()).Returns(5);
Mock.Arrange(() => fakeEntities.SomeObjectSet.ToList()).Returns(someList);

The first arrange works fine, but the ToList() call results in an ArgumentNullException.

It looks like it is actually trying to convert 'SomeObject' to a list instead of getting mocked. Any Ideas?
Ricky
Telerik team
 answered on 18 May 2011
1 answer
215 views
I am having similar issues as those in this thread:
http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

Basically, I have some unit tests that work fine when i run them alone, together however, the first test passes, then the rest fail. It seems as if the first tests mocks are successfully intercepted, and then the subsequent tests mocks are not.

The solution it seems is the Mock.Initialize function. Does Mock.Initialize apply to class methods that are not static? If so, is there an example I could take a look at?

Ricky
Telerik team
 answered on 16 May 2011
3 answers
121 views
Hello,

I'm currently using the trial version to investigate if we want to use JustMock as our new mocking framework.
We especially like the 'Elevated' mode where we can mock mscorlib classes, etc.

However when I try to run the justmock tests (MSTest in VS2008) the Visual Studio test runner stops and throws the following error:
"Unit Test Adapter threw exception: Bad class token.."

This is my dev setup:
Windows XP
Visual Studio 2008
.NET Framework 3.5 SP1
JustMock trial

Is there a solution for this problem? If so, please let me know asap so that I can further investigate this tool.

Thanks in advance for your help.
Ruud
Ricky
Telerik team
 answered on 05 May 2011
1 answer
126 views
 public class Class1
    {
        private bool Helper()
        {
            return true;
        }

        public bool TestMe()
        {
            if (Helper()) return true;

            return false;

        }

    }

 [TestMethod]
        public void TestMethod1()
        {

            var class1 = Mock.Create<Class1_Accessor>();
            Mock.Arrange(() => class1.Helper()).Returns(false);
            Mock.Arrange(() => class1.TestMe()).CallOriginal();
            bool actual = class1.TestMe();
            Assert.IsFalse(actual);

        }

Why is the class1.Helper() mock not mocking, it is executing the method.  We have legacy code which has this going on everywhere so I want mock internal calls.

Thanks.
Ricky
Telerik team
 answered on 27 Apr 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?