Hello,
I'm trying to test if a method on a service proxy is called. Apparently I'm missing something, and I need some help to get this working.
This is the code I wish to test (very simple and stripped down!):
public void CreateDebtor(AxdEntity_CustTable debtor)
{
HndDebtorsService service;
string username = Thread.CurrentPrincipal.Identity.Name;
WcfService custService = new WcfService("WSHttpBinding_HndDebtorsService", typeof (HndDebtorsServiceChannel), null);
OperationContextScope oScope = new OperationContextScope((IClientChannel) custService.Channel);
using (oScope)
{
AuthenticationHelper.SetHeader(username);
service = factory.CreateInstance<HndDebtorsService>(custService);
debtor.@class = "entity";
HndDebtorsServiceCreateRequest customerServiceCreateRequest = new HndDebtorsServiceCreateRequest();
AxdHndDebtors debtors = new AxdHndDebtors {CustTable = new AxdEntity_CustTable[1]};
debtors.CustTable[0] = debtor;
customerServiceCreateRequest.HndDebtors = debtors;
Logger.Instance.Log("About to save customer", LoggerEnums.Severity.DEBUG, debtor);
// I want to verify that service.create is called!
HndDebtorsServiceCreateResponse response = service.create(customerServiceCreateRequest);
Logger.Instance.Log("Customer saved, response:", LoggerEnums.Severity.DEBUG, response.EntityKeyList);
}
}
HndDebtorsService is an interface, it's just the naming which is horrible.
So, my initial thoughts in verifying that service.create is called was something like this:
[Test, Isolated]
public void CreateDebtor_CreatingDebtor_CreateIsCalled()
{
// ARRANGE
RS_DebtorService service = new RS_DebtorService();
HndDebtorsService fakeService = Isolate.Fake.Instance<HndDebtorsService>();
Isolate.Swap.NextInstance<HndDebtorsService>().With(fakeService);
Isolate.WhenCalled(() => fakeService.create(null)).WillReturn(null);
// ACT
service.CreateDebtor(null);
// ASSERT
Isolate.Verify.WasCalledWithAnyArguments(() => fakeService.create(null));
}
Unfortunately this doesn't work since I'm not able to fake an interface.
So, next try was to create a fake implementation of the interface, and swap the next instance of the interface with the fake implementation. Unfortunately I'm getting an error saying "Item has already been added. Key in dictionary: 'dm' Key being added: 'dm'"
So, any suggestions?