This is a migrated thread and some comments may be shown as answers.

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

2 Answers 15 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sebastian
Top achievements
Rank 1
Sebastian asked on 20 Mar 2013, 12:08 PM
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 Answers, 1 is accepted

Sort by
0
Sebastian
Top achievements
Rank 1
answered on 20 Mar 2013, 12:48 PM
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.


0
Accepted
Kaloyan
Telerik team
answered on 25 Mar 2013, 09:44 AM
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.
Tags
General Discussions
Asked by
Sebastian
Top achievements
Rank 1
Answers by
Sebastian
Top achievements
Rank 1
Kaloyan
Telerik team
Share this question
or