Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not matching public properties? #30

Closed
eschwartz opened this issue Oct 31, 2016 · 4 comments
Closed

Not matching public properties? #30

eschwartz opened this issue Oct 31, 2016 · 4 comments

Comments

@eschwartz
Copy link

I'm attempting to create a mock for AWS.S3, but I'm getting a error:

error TS2322: Type 'Mock<S3>' is not assignable to type 'S3'.
  Property 'endpoint' is missing in type 'Mock<S3>'.

My code looks like:

import {S3} from "aws-sdk";
import * as TypeMoq from 'typemoq';

function createS3ClientMock():S3 {
  const mock:TypeMoq.Mock<S3> = TypeMoq.Mock.ofType(S3);

  return mock;
}

The definition for S3 looks like:

export declare class S3 {
    constructor(options?: any);
    endpoint: Endpoint;

    getObject(params: s3.GetObjectRequest, callback?: (err: Error, data: any) => void): any;
    putObject(params: s3.PutObjectRequest, callback: (err: Error, data: any) => void): void;
    deleteObject(params: s3.DeleteObjectRequest, callback: (err: Error, data: any) => void): void;
    headObject(params: s3.HeadObjectRequest, callback: (err: Error, data: any) => void): void;
    getSignedUrl(operation: string, params: any): string;
    getSignedUrl(operation: string, params: any, callback: (err: Error, url: string) => void): void;
    upload(params?: s3.PutObjectRequest, options?: s3.UploadOptions, callback?: (err: Error, data: any) => void): void;
    listObjects(params: s3.ListObjectRequest, callback: (err: Error, data: s3.ListObjectResponse) => void): void;
    listObjectsV2(params: s3.ListObjectV2Request, callback: (err: Error, data: s3.ListObjectV2Response) => void): void;
    waitFor(state: string, params: s3.HeadObjectRequest, callback: (err: Error, data: any) => void): void;

    createMultipartUpload(params: any, callback: (err: Error, data: any) => void): void;
    uploadPart(params: any, callback: (err: Error, data: any) => void): void;
    listParts(params: any, callback: (err: Error, data: any) => void): void;
    completeMultipartUpload(params: any, callback: (err: Error, data: any) => void): void;
  }

I'm able to hack my way around this, by doing something like:

function createS3ClientMock():S3 {
  const mock: any = TypeMoq.Mock.ofType(S3);
  mock.endpoint = 'STUBBED_ENDPOINT';
  return <S3> mock;
}
@dizel3d
Copy link

dizel3d commented Oct 31, 2016

function createS3ClientMock():S3 {
  const mock: any = TypeMoq.Mock.ofType(S3);

  // it's wrong way to stub. you should use mock.setup(...)
  mock.endpoint = 'STUBBED_ENDPOINT';

  // you should not cast mock to S3, you should return mock.object
  return <S3> mock;
}

Read README, it's clear enough.

@florinn
Copy link
Owner

florinn commented Nov 8, 2016

@eschwartz Here is your example modified as suggested by @dizel3d

import { S3, Endpoint } from "aws-sdk";
import * as TypeMoq from "typemoq";

function createS3ClientMock(): S3 {
    const mock = TypeMoq.Mock.ofType(S3);

    // it's wrong way to stub. you should use mock.setup(...)
    mock.setup(x => x.endpoint).returns(() => new Endpoint("STUBBED_ENDPOINT"));

    // you should not cast mock to S3, you should return mock.object
    return mock.object;
}

console.log(createS3ClientMock().endpoint);
//Endpoint {
//  protocol: 'https:',
//  host: 'stubbed_endpoint',
//  port: 443,
//  hostname: 'stubbed_endpoint',
//  pathname: '/',
//  path: '/',
//  href: 'https://stubbed_endpoint/' }

@florinn florinn closed this as completed Nov 8, 2016
@eschwartz
Copy link
Author

Thanks, @florinn for the example.

I kind of the gave up on this, because the documentation on setup is confusing to me. What is the signature of setup? In your example, what is x and why does the function passed to mock.setup return x.endpoint? What does that do?

@florinn
Copy link
Owner

florinn commented Nov 9, 2016

setup accepts a function (also referred to as 'matcher') taking as input argument the type being mocked (here x having type S3) and as body the value/property/method (with arguments if that's the case) to match (here the getter x.endpoint).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants