Telerik Forums
JustMock Forum
5 answers
116 views
[TestFixture]
public class Class1
{
    public class Individual
    {
         
    }
    public class Family : Collection<Individual>
    {
         
    }
    public interface ITest
    {
        int Test(Family family);
    }
 
    [Test]
    public void Test()
    {
        var family1 = new Family();
        var family2 = new Family();
 
        var test = Mock.Create<ITest>();
        Mock.Arrange(() => test.Test(family1)).Returns(1).MustBeCalled();
        Mock.Arrange(() => test.Test(family2)).Returns(2).MustBeCalled();
 
        var result = test.Test(family1) + test.Test(family2);
 
        Mock.Assert(test);
    }
}
In the code as written I get an error:
Occurrence expectation failed. Expected at least 1 call. Calls so far: 0
Arrange expression: () => test.Test(family1).

However, when I change the code to not have Family based on Collection<Individual> it works.

This does not seem correct.  Is this a bug, or am I missing something on how JustMock is matching these parameters?

Eric Gurney
Stefan
Telerik team
 answered on 24 Jul 2014
5 answers
99 views
Greetings,

I downloaded JuckMocked, open the VB.NET solution, did a build and got a good deal of errors in various statements in regards to Lambda inline functions. Now I could go in and figure them out easily but I am not here for that but instead to evaluate this product hence my thread.

BTW: No issues with the C# solution.

Open with VS2012 Ultimate
Kaloyan
Telerik team
 answered on 18 Jul 2014
1 answer
179 views
Am I correct in saying that when using the AutoMocking container to assert a call on a dependency, you *have* to create some form of expectation which is then asserted (e.g. with AssertAll), rather than just defining the assertion at the end?

For example, the following works:
[TestMethod]
public void Test()
{
 var c = new MockingContainer<Target>();
 c.Arrange<ITargetDependency>(x => x.ShouldBeCalled());

 c.Instance.WorkYourMagic();

 c.AssertAll(); // will fail if WorkYourMagic does not call ITargetDependency.ShouldBeCalled()
}

However, this does not...
[TestMethod]
public void Test()
{
 var c = new MockingContainer<Target>();

 c.Instance.WorkYourMagic();

 c.Assert<ITargetDependency>(x => x.ShouldBeCalled()); // Always claims WorkYourMagic does not call ITargetDependency.ShouldBeCalled()
}

Regards,
Nick
Stefan
Telerik team
 answered on 18 Jul 2014
1 answer
104 views
The following works:
using NUnit.Framework;
using Telerik.JustMock.AutoMock;
 
namespace AutoMockBug
{
    public interface IBar
    {
        void DoNothing();
    }
 
    public class Baz
    {
        public IBar Bar;
        public Baz(IBar bar)
        {
            Bar = bar;
        }
    }
 
    [TestFixture]
    public class Class1
    {
        [Test]
        public void test()
        {
            var container = new MockingContainer<Baz>();
            container.Arrange<IBar>(x => x.DoNothing()).OccursOnce();
 
            container.Instance.Bar.DoNothing();
 
            container.Assert();
        }
    }
}

This doesn't work:
using NUnit.Framework;
using Telerik.JustMock.AutoMock;
 
namespace AutoMockBug
{
    public interface IFoo
    {
        IBar GetBar();
    }
 
    public interface IBar
    {
        void DoNothing();
    }
 
    public class Baz
    {
        public IFoo Foo;
        public Baz(IFoo foo)
        {
            Foo = foo;
        }
    }
 
    [TestFixture]
    public class Class1
    {
        [Test]
        public void test()
        {
            var container = new MockingContainer<Baz>();
            container.Arrange<IBar>(x => x.DoNothing()).OccursOnce();
 
            container.Instance.Foo.GetBar().DoNothing();
 
            container.Assert();
        }
    }
}
Stefan
Telerik team
 answered on 18 Jul 2014
4 answers
312 views
On another forum I found out how to mock the Operational context in this snippet

what I want to do is mock the endpoint in it now, My unit test here needs to test all aspects and I am having issues actually mocking the endpoint

truthfully I do not know what needs to be mocked or how but I know what I want the end result to be. I just need to be able to get endpoint to not be null and set
retVal = endpoint.Address;
Ive tried so many things that I could not get a result 

The code:

        public static string GetCallerIPAddress()
        {
            string retVal = null;
            if (OperationContext.Current != null)
            {
                MessageProperties prop = OperationContext.Current.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                if (endpoint != null)
                {
                    retVal = endpoint.Address;
                }
            }
            return retVal;
        }
Stefan
Telerik team
 answered on 18 Jul 2014
3 answers
134 views
01.public class Foo
02.{
03.    public Bar Bar;
04. 
05.    public Foo(Bar bar)
06.    {
07.        Bar = bar;
08.    }
09.}
10. 
11.public class Bar
12.{
13.    public IBaz Baz;
14. 
15.    public Bar(IBaz baz)
16.    {
17.        Baz = baz;
18.    }
19.}
20. 
21.public interface IBaz
22.{
23.    String GetString();
24.}
25. 
26.[Test]
27.public void when()
28.{
29.    var foo = new MockingContainer<Foo>();
30.    foo.Arrange<IBaz>(baz => baz.GetString()).Returns("Rawr!");
31. 
32.    var actualResult = foo.Instance.Bar.Baz.GetString();
33. 
34.    Assert.AreEqual("Rawr!", actualResult);
35.}

Given the above code, a null reference exception is thrown on line 32.

I am using NInject to do constructor dependency injection.  I have a large hierarchy of classes that take in their dependencies in their constructors as Foo and Bar do above.  I want all of my concrete classes to be instantiated and only have the interfaces get mocked.  In the above example, I want a real Bar constructed and a real Foo constructed but I want IBaz to be mocked.

I am aware that in the above scenario I could just manually construct a Foo, passing in a manually constructed Bar, but in my scenario, the dependency hierarchy is large and complex and interfaces wrap all external (not my code calls).  Is there an easy way to tell the MockingContainer<Foo> that it should mock all interfaces but not mock any concrete classes (actually instantiate them).

As I said, I am using Ninject to instantiate my classes and it does the right thing everywhere.  I understand that the automocking container is backed by Ninject.  Is there a way I can bind certain interfaces/classes to JustMock?  In the above example I could do something like:

kernel.Bind<IBaz>().To<Mock<IBaz>>()?  That way I could just use Ninject to instantiate my object hierarchy, but tell it which interfaces I want it to bind to JustMock mocked instances.
Stefan
Telerik team
 answered on 18 Jul 2014
15 answers
211 views
Hi all,

 I was wondering if JusyMock could easily "intercept" object creation and replace them with fakes created on the fly?

I'm trying to unit test a method that, amongs other things, creates an instance of an object of a certain type and then uses it.  The instance is NOT injected into the method and so I cannot control what instance is used inside the method...

I know that some other mocking framework do support Future Objects mocking.  Is it the case with JustMock ?

Thanks for your time,

Vincent Grondin

EDIT:   I think I need the "Weaver" library to accomplish this, can someone from telerik tell me if this library is included in the FREE version of JustMock? 
Kaloyan
Telerik team
 answered on 16 Jul 2014
3 answers
363 views
Hi,

Looking at getting JustMock but have a simple license question. If say in a dev team of 20, only 10 would write unit tests, but the unit tests are in the solution, would the remaining 10 developers that don't have a JustMock license be able to build and run the unit tests?

Thanks for your help

Graham
Joseph
Telerik team
 answered on 14 Jul 2014
1 answer
145 views
I'm using Visual Studio 2013 MSTest
JustMock Lite 2014.2.609.3 (via NuGet)

I'm trying to move the "MustBeCalled" expectations to TestCleanup.
Here is an example of code.

01.[TestClass]
02.public class TestFixture
03.{
04.  private ICloneable _svc;
05. 
06.  [TestInitialize]
07.  public void TestSetup() { _svc = Mock.Create<ICloneable>(); }
08. 
09.  [TestCleanup]
10.  public void TestCleanup() { Mock.Assert(_svc); }
11. 
12.  [TestMethod]
13.  public void TryStuff()
14.  {
15.    // arrange
16.    _svc = Mock.Create<ICloneable>();
17.    Mock.Arrange(()=> _svc.Clone()).MustBeCalled();
18. 
19.    // act
20.    //_svc.Clone();
21. 
22.    // assert
23.  }
24.}

If I run the code as is, the test will pass (regardless if line 20 is commented or not).

This can be fixed by moving the Mock.Assert to line 22+ but ideally I would like to "auto call" Mock.Assert at the end of every test.  I thought that adding it to TestCleanup would work but it doesn't appear to do so.

It appears that prior to TestCleanup running, JustMock will clear out the expectations prior to running TestCleanup.
Is there anyway around this?

Why am I doing this?
Because I'd actually like to write tests like this.

01.public class TestFixture
02.{
03.  private List<object> _assertList = new List<object>();
04. 
05.  [TestCleanup]
06.  public void TestCleanup() { foreach (var svc in _assertList) { Mock.Assert(svc); } }
07.   
08.  private void CloneableMustBeCalled()
09.  {
10.    Mock.Arrange(() => _svc.Clone()).MustBeCalled();
11.    if (!_assertList.Contains(_svc)) { _assertList.Add(_svc); }
12.  }
13.}

Of course my actual test classes will be more complex. But this approach allows me to Mock out MustBeCalled expectations in a more readable manner.  Currently, the test author will have to remember to call Assert the _svc class (or _assertList) at the end of every TestMethod, but was hoping for a more maintainable solution.

Thanks
Stefan
Telerik team
 answered on 10 Jul 2014
3 answers
2.9K+ views
If this has been answered already you can just direct me there...

In the method i am working on I am trying to mock a "checkpoint".

the line of code is this....    if (File.Exists(filePath)){
rest of code is here
}
I need to mock this so the program just moves on regardless of if there is a file there or not.

Any suggestions?
Matthew
Top achievements
Rank 1
 answered on 10 Jul 2014
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?