Don't get the error...need some help

3 posts, 1 answers
  1. Sebastian
    Sebastian avatar
    16 posts
    Member since:
    Jun 2010

    Posted 20 Mar 2013 Link to this post

    Sorry for the meaningless title, but I do have a general question on using JustMock.

    Imagine the following SUT

    public interface IDao
    {
      // throws an exception on duplicate key
      void Insert(string key, string value);
      // throws an exception if key not available
      void Update(string key, string value);
      // throws an exception of key not found
      string FindByKey(string key);
    }
     
    public interface IService
    {
      string Get(string key);
      void Set(string key, string value);
      // should insert or update the value for the given key
      void Save(string key, string value);
    }
     
    public class ServiceImpl : IService
    {
      private readonly IDao _dao;
     
      public ServiceImpl(IDao dao)
      {
        _dao = dao;
      }
       
      public string Get(string key)
      {
        return _dao.FindById(key);
      }
     
      public void Set(string key, string value)
      {
        _dao.Insert(key, value);
      }
       
      public void Save(string key, string value)
      {
        var value = Get(key);
        if (null == value)
        {
          Set(key, value);
        }
        else
        {
          _dao.Update(key, value);
        }
      }
    }

    Now have a look at my test case for the save method:
    [Test]
    public void SaveShouldCallInsertOnNonExistingKey()
    {
      var key = "key";
      var value = "value";
      var dao = Mock.Create<IDao>();
      dao.Arrange(x => x.Insert(Arg.AnyString,Arg.AnyString)).MustBeCalled();
      var service = Mock.Create<ServiceImpl>(Behaviour.CallOriginal, dao);
      service.Arrange(x => x.Get(Arg.AnyString)).Returns((string)null).MustBeCalled();
       
      service.Save(key, value);
       
      Mock.AssertAll(dao);
      Mock.AssertAll(service);
    }

    Running this test I get the error that Mock.AssertAll(dao) failed because the IDao.Insert method has never been called ?

    Debugging through the test I could step into

    service.Save()
    -> service.Get() => returning NULL as expected
    -> service.Set()
      -> dao.Insert (which is proxied)


    What am I doing wrong here ?

    Kind regards
    Sebastian



  2. Sebastian
    Sebastian avatar
    16 posts
    Member since:
    Jun 2010

    Posted 20 Mar 2013 Link to this post

    Sorry, finally found the problem myself.

    I've tested with the code provided in this post for simplicity and not have to share the complete real classes I'm currently testing.
    And this was exactly the problem.

    In the original interface the Insert-Method is declared as

    Insert(KeyValuePair<string,string> kvp)

    The mock has been arranged as

    var kvp = new KeyValuePair<string,string>(....);
    dao.Arrange(x => x.Insert(kvp)).MustBeCalled();

    Within the set method I do

    public void Set(string key, string value)
    {
       ...
       _dao.Insert(new KeyValuePair<string,string>(key, value));
    }

    The problem is that I've thought the mocking framework would check on Equility of the KeyValuePair<string,string>, but it seems it checks in the instance ?

    If I arrange like

    dao.Arrange(x => x.Insert(Arg.IsAny<KeyValuePair<string,string>>()))....

    everything works find.


  3. DevCraft R3 2016 release webinar banner
  4. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 25 Mar 2013 Link to this post

    Hello Sebastian,

    I apologize for the late reply.

    However, I am happy that you have found a solution to the matter by yourself.

    I managed to reproduce the issue and I would note, you are correct that the following arrange:
    var kvp = new KeyValuePair<string,string>(....);
    dao.Arrange(x => x.Insert(kvp)).MustBeCalled();
    , does not match the original call:
    _dao.Insert(new KeyValuePair<string,string>(key, value));

    This appears to be a bug in JustMock, which we were not aware of. Our development team is going to investigate it further and provide a fix for future releases.

    For reporting such an issue I have granted you some Telerik points.

    Please, contact us again if you need more help.

    All the best,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top