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

Use mocks for tests #7

Closed
danielolivaresd opened this issue Sep 15, 2017 · 1 comment
Closed

Use mocks for tests #7

danielolivaresd opened this issue Sep 15, 2017 · 1 comment

Comments

@danielolivaresd
Copy link

I am using unit and end to end tests on my app (with Karma, Jasmine and Protactor by following ionic-unit-testing-example).

Let's say I have a class that calls the actual Ionic Native wrapper for a plugin, but I want to test this component. For tests, I'd like to use the mock for that Ionic Native wrapper, instead of the wrapper itself. Please see the example below:

// My class using an Ionic Native wrapper
import { BLE } from '@ionic-native/ble';

export class ClassToBeTested {
  uuid: string
  ble: BLE

  constructor (uuid) {
    this.uuid = uuid
    this.ble = new BLE()
  }

  connect () {
    this.ble.connect(this.uuid)
    // ...
  }
}
// Test:
import { ClassToBeTested } from "../../lib/class_to_be_tested"

describe('ClassToBeTested', () => {
  it('can be instantiated', () => {
    let instance:ClassToBeTested = new ClassToBeTested("a")
    expect(instance instanceof ClassToBeTested).toBe(true)
    expect(instance.uuid).toBe("a")
  })

  it('can connect', () => {
    let instance:ClassToBeTested = new ClassToBeTested("a")
    expect(instance.connect).not.toThrow()
    // This fails: Expected function not to throw, but it threw TypeError: Cannot read property 'ble' of undefined.
  })
})

I think that what I need is to use BLEMock instead of BLE when testing. How can this be done?

@danielolivaresd
Copy link
Author

Closing this since this blog post and writing my own mocks that use the Ionic Native mocks + Jasmine's spyOn() seem to do the trick for me.

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

1 participant