Skip to content

Instantly share code, notes, and snippets.

@poetix
Created September 25, 2017 12:39
Show Gist options
  • Select an option

  • Save poetix/3447653ad0954d4ecd9c457447240751 to your computer and use it in GitHub Desktop.

Select an option

Save poetix/3447653ad0954d4ecd9c457447240751 to your computer and use it in GitHub Desktop.

Revisions

  1. poetix created this gist Sep 25, 2017.
    48 changes: 48 additions & 0 deletions structural_typing3.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    import { expect } from 'chai';
    import 'mocha';
    import { mock, instance, when, anyString, verify } from "ts-mockito";

    class HelloEndpoint {
    constructor(private service: HelloService) {}

    async handle(evt: any): Promise<any> {
    const greeting = await this.service.sayHello(evt.queryStringParameters.name);
    return {
    body: greeting
    };
    }
    }

    // Concrete implementation
    class HelloService {

    async sayHello(name: string): Promise<string> {
    throw Error("Not implemented yet")
    }

    }

    const mockedHelloService: HelloService = mock(HelloService);
    const helloService: HelloService = instance(mockedHelloService);

    when(mockedHelloService.sayHello(anyString()))
    .thenCall(name => Promise.resolve(`Hello, ${name}!`));

    const helloEndpoint = new HelloEndpoint(helloService);

    describe("The Hello Endpoint", () => {
    it("Should greet the person", async () => {
    // Call the endpoint.
    const result = await helloEndpoint.handle({
    queryStringParameters: {
    name: "Arthur Putey"
    }
    });

    // Validate that the right data is passed into the service.
    verify(mockedHelloService.sayHello("Arthur Putey")).once();

    // Validate that the right response is returned to the client.
    expect(result.body).to.equal("Hello, Arthur Putey!");
    });
    });