Telerik Forums
JustMock Forum
0 answers
80 views
Hello all,

I try to test if controller object has assigned a method to an event (see sample code at the end). 

I have a controller object, which gets two objects (getCalled & raiseEvent) via an interface. The object getCalled provides a 
method which should be assigned to an event of the object raiseEvent. This should be preformed from within the controller object.
The event can only occur after the raiseEvent.Start method has been called.

I thought, I write an Arrange statement, where the method getCalled.callMe must be called.
Additionally I write an Arrange statement, where an event should be raised, when the raiseEvent.Start method gets called.
Now I create the controller and call the method controller.Start. In this method the the method raiseEvent.Start will be called, which should rasie the Event raiseEvent.calledEvent, which should call the method getCalled.CallMe.
But the testrunner asserts with the message
"Setup contains calls that are marked as "MustBeCalled" but actually never called"

What do I make wrong?

best regards
  Bernhard

Here is the sample code

    using NUnit.Framework;

    using Telerik.JustMock;
    using Telerik.JustMock.Helpers;

    public interface IGetCalled
    {
        void CallMe(EventArgs arg);
    }

    public interface IRaiseEvent
    {
         event EventHandler<EventArgs> calledEvent;
         void Start();
    }

    public class Controller
    {
         private IRaiseEvent raiseEvent;
         private IGetCalled  getCalled;

         public Controller(IGetCalled getCalled, IRaiseEvent raiseEvent)
         {
             this.raiseEvent = raiseEvent;
             this.getCalled  = getCalled; 
     
             this.raiseEvent.calledEvent += (s, e) => this.getCalled.CallMe(e);         
         }
                        
         public void Start()
         {
             raiseEvent.Start();   
         }
    }

    [TestFixture]
    public class test
    {
        [Test]
        public void TestRaiseEvent()
        {
            IRaiseEvent raiseEvent = Mock.Create<IRaiseEvent>();
            IGetCalled getCalled = Mock.Create<IGetCalled>();

            getCalled.Arrange(x => x.CallMe(new EventArgs())).MustBeCalled();
            Mock.Arrange(() => raiseEvent.Start()).Raises(() => raiseEvent.calledEvent += null, new EventArgs());
            var controller = new Controller(getCalled, raiseEvent);

            controller.Start();
            getCalled.Assert();
        }
    }
Bernhard
Top achievements
Rank 1
 asked on 01 Oct 2012
4 answers
89 views
I have a method that uses an OpenAccess model. I want to mock out an OptimisticVerificationException in the first (and only first) call to SaveChanges(). How can this be achieved?

public void ThrowTest(Guid id, string stringToUpdate)
       {
           var user = _model.Users.SingleOrDefault(p => p.Guid == id);
           if (user != null)
           {
               user.StringToUpdate = stringToUpdate;
               try
               {
                   _model.SaveChanges();
               }
               catch (OptimisticVerificationException optimisticVerificationException)
               {
                   _model.Refresh(RefreshMode.OverwriteChangesFromStore, user);
                   user.StringToUpdate = stringToUpdate;
                   _model.SaveChanges();
               }
 
           }
       }
Ricky
Telerik team
 answered on 26 Sep 2012
1 answer
75 views
Hi,
we are having problems running out tests in large testruns.
In this forum I read, that I have to use the Mock.Initialize<T> Method to init the Object I like to mock, if I had instances of this class in preceding tests.
Do I have to call this Initialize Method also for the base classes, my object ist derived from?

Thank you very much
Christian
Ricky
Telerik team
 answered on 21 Sep 2012
1 answer
60 views
I have a unit test that has two calls to SaveChanges() when the first attempt to call SaveChanges() throws an OptimisticVerificationException, a refresh of the object occurs and then a second attempt is made. When writing my unit test I arranged the following expectations:

Mock.Arrange(() => _mockModel.SaveChanges()).Occurs(2);
 
Mock.Arrange(() => _mockModel.SaveChanges()).Throws<OptimisticVerificationException>(string.Empty).InSequence();
 
Mock.Arrange(() => _mockModel.SaveChanges()).InSequence();
 
Mock.Arrange(() => _mockModel.Users.SingleOrDefault(p => p.Guid == userGuid)).Returns(new User());
 
Mock.Arrange(() => _mockModel.Refresh(RefreshMode.OverwriteChangesFromStore, Arg.IsAny<User>())).MustBeCalled();

What is interesting is that the call to: Mock.Arrange(() => _mockModel.SaveChanges()).Occurs(2); actually breaks the next two lines (the one that throws on the first SaveChanges() in sequence). Regardless of where Occurs(n) appears in the arrange, it breaks the throw. Simply commenting it out allows the test to run as expected.

Am I doing something wrong or is there a defect here?

Ricky
Telerik team
 answered on 21 Sep 2012
23 answers
354 views
I get this exception "Telerik.JustMock.MockException: Opps , there were some error intercepting target call" when attempting to setup a static mock:

Mock.SetupStatic<MyTest>();
Mock.Arrange(() => MyTest.Parse("2")).Returns(new MyTest());

the class I'm mocking is very simple:

public class MyTest
{
    public static MyTest Parse(string value)
    {
        return new MyTest();
    }
}

Ricky
Telerik team
 answered on 20 Sep 2012
1 answer
484 views
Hi there,
I am trying to mock the InnerException-Property of a Mocked SqlException.

[Test]
public void SqlException_Example()
{
    var sqlEx = Mock.Create<SqlException>();
    sqlEx.Arrange(ex => ex.Class).Returns(20);
    sqlEx.Arrange(ex => ex.InnerException).Returns(new Exception("My Test"));
 
    Assert.IsTrue(Mock.IsProfilerEnabled);
    Assert.That(sqlEx.Class, Is.EqualTo(20));
    Assert.That(sqlEx.InnerException, Is.Not.Null); //<-- Fails
}


I will be grateful for any help.

Normen

Ricky
Telerik team
 answered on 13 Sep 2012
4 answers
4.0K+ views
Hi there!

Let's suppose I have a very simple class like this:

public class SimpleClass
{
public string GetFormatedString(string mask, params object[] par) 
{
return string.Format(mask, par);
}
}

I honestly have tried to Mock this method considering the fact that it may be called with an unpredictable number of arguments on "par" when running the tests. But I was unable to find something like this on JustMock documentation. I found many examples where you have to set exactly the number of arguments passed. Also mocking the second parameter as Args.IsAny<object[]>() doesnt work.

Can anyone help me on this question, please?
Christiano
Top achievements
Rank 2
 answered on 12 Sep 2012
2 answers
287 views
[TestMethod]
public void MyTestMethod()
{   
// here when I look at nodes, it has values
XmlNodeList nodes = GetPresetNodes();
XmlDocument mockedXmlDocument = Mock.Create<XmlDocument>();

Mock.Arrange(() => mockedXmlDocument.Load("C:\\Presets.xml"))
.IgnoreInstance()
.DoNothing();
 
// it doesn't matter if I have IgnoreArguments here or not
Mock.Arrange(() => mockedXmlDocument.SelectNodes("//presets/preset"))
.IgnoreArguments()
        .IgnoreInstance()
.Returns(presetNodes);

ClassA c = new ClassA();

}

public class ClassA
{
public static readonly ClassToTest = new ClassToTest();
....
}

public sealed class ClassToTest
{
// constructor
public ClassToTest()
{
XmlDocument doc = new XmlDocument();

// this correctly gets ignored
doc.Load("
C:\\Presets.xml");

// this returns an empty XmlNodeList and not the one I created in the test
XmlNodeList presets = doc.SelectNodes("//presets/preset");
}
}

Any Ideas?


Ricky
Telerik team
 answered on 12 Sep 2012
1 answer
111 views
Can I check-in the JustMock assemblies into souce control and reference these from the application assemblies? Will unit tests sill work?

My installation folder is C:\Program Files (x86)\Telerik\JustMock\Libraries. Can I copy these assemblies into my source tree and reference these from my test projects? Will the tests still run (that use the profiler) on the build server? If so, is there any special set up to make this work?
Ricky
Telerik team
 answered on 10 Sep 2012
1 answer
135 views
It is possible to mock non-public properties?

It doesn't appear that Mock.NonPublic permits referencing non-public properties. For example, there appears to be no ArrangeGet or ArrangeSet.
Kaloyan
Telerik team
 answered on 10 Sep 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?