Re: [nsubstitute] Help me to solve this exception: "Expected to receive exactly 1 call matching: MyMethod Actually received no matching calls."

2,196 views
Skip to first unread message

David Tchepak

unread,
Jun 15, 2012, 8:15:51 PM6/15/12
to nsubs...@googlegroups.com
The problem is in this bit of the test:

            //When
            MyEntities actual = concreteDal.GetMyEntities(Arg.Any<string>());

The argument matchers like `Arg.Any<string>()` can only be used with substitutes. In the above call it is used with a call to a concrete class. You can fix this by passing through a real argument to the concrete call:

            MyEntities actual = concreteDal.GetMyEntities("test");

I'll have a look to see if there's anything we can do to throw a more helpful error in these cases, because the current one is quite misleading.

Cheers,
David

On Sat, Jun 16, 2012 at 1:29 AM, Aurelien <theba...@gmail.com> wrote:
Hello,
I'm using NSubstitute 1.4.0.0. I try to test my method: GetMyEntities(string myId). But, I have this exception: "Expected to receive exactly 1 call matching: LoadData(any GetCommand) Actually received no matching calls.", when my ReceivedWithAnyArgs assertion for LoadData method is called. Have you got an idea to solve it ? Please help me.
  • My Code:
public class DataAccessLayer : IDataAccessLayer, IDisposable
{
 private IBusinessEntities dataFacade;       
 public DataAccessLayer(IBusinessEntities facade)
 {
  dataFacade = facade;
 }
 public MyEntities GetMyEntities(string myId)
 {
  GetAllMyEntities getMyEntities = new GetAllMyEntities();
  getMyEntities.Id = myId;
  dataFacade.LoadData(getMyEntities);
  return dataFacade.Get<MyEntities>();
 }
}
 

public interface IBusinessEntities

{
 T Get<T>() where T : BusinessEntitiesBase, new();
 void LoadData(GetCommand commandName);
}
 
  • My Test:
public void GetMyEntitiesOK_UndefinedParam_MyEntitiesIsSame()
{
 //Given
 var mockFacade = Substitute.For<IBusinessEntities>();
 var concreteDal = new DataAccessLayer(mockFacade);
 MyEntities expected = new MyEntities();
 //Expect
 mockFacade.Get<MyEntities>().ReturnsForAnyArgs(expected);
 //When
 MyEntities actual = concreteDal.GetMyEntities(Arg.Any<string>());
 //Then
 Assert.AreSame(expected, actual);
 mockFacade.ReceivedWithAnyArgs(1).LoadData(Arg.Any<GetCommand>()); //exception????
 mockFacade.ReceivedWithAnyArgs(1).Get<MyEntities>();
}
 

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nsubstitute/-/0Izru44BMMAJ.
To post to this group, send email to nsubs...@googlegroups.com.
To unsubscribe from this group, send email to nsubstitute...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nsubstitute?hl=en.

Aurelien

unread,
Jun 19, 2012, 9:49:31 AM6/19/12
to nsubs...@googlegroups.com
PERFECT !!! Thanks you so much. NSubstitute is really an excellent mocking framework.
 
Cheers,
Aurélien
To unsubscribe from this group, send email to nsubstitute+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages