I think it was the generics (yay we can pass objects around w/o having to know their type - how awesome! but types are still good because reasons) I really struggled with. Here's the guts of what I had to do:
Code:
public class Handler
{
static AppFxClient BBClient;
public Handler()
...
// virtual so it can be overriden by test hander
// ideally eventually will pull from a service class that can be overridden to return mock services with env vars
public virtual IDataList<EmailInput, LookupOutput> getDataList(AppFxClient client, Guid callId)
{
return new DataList<EmailInput, LookupOutput>(client, callId);
}
I had to create the IDataList interface, which MockDataList and DataList (original code) implement.
Interface is super simple (minus the generics, which took a long time to wrap my head around):
Code:
public interface IDataList<InputsT, OutputsT>
where OutputsT : new()
where InputsT : class
{
Task<IEnumerable<OutputsT>> LoadAsync(InputsT inputs, DataListLoadOptions options);
}
}
So the test handler extends the lambda handler and a testing interface and overrides the getDataList method:
Code:
public class HandlerTest : Handler, ILambdaTest
{
// xunit tests
[Fact]
public async void SingleUserSuccessTest()
...
// overridden from Handler
// ideally eventually will pull from a service class that can be overridden to return mock services with env vars
public override IDataList<EmailInput, LookupOutput> getDataList(AppFxClient client, Guid callId)
{
var mockDataList = new MockDataList<EmailInput, LookupOutput>(client, callId);
mockDataList.HandlerTest = this;
return mockDataList;
}
Lotta hoops man. Here's some code for a node test that passes a mock DB client to the handler (in regular code the handler takes a real DB client and doesn't know or care the difference):
Code:
const docClientMock = {
put: function(params, callback) {
if (params.Item.UserId === "test-user-already-added") callback(dupeUserErrorResponse);
else callback(null, successResponse);
}
};
describe('Tests index', function() {
it('verifies successful response', function(done) {
saveNewUser(newUser, docClientMock, (err, result) => {
test.value(err).is(null);
test.object({success:true}).is(result);
done();
});
});
});
;
This must feel like cheating to you.
Last edited by suzzer99; 12-06-2018 at 10:46 PM.