thlorenz / proxyquire

🔮 Proxies nodejs require in order to allow overriding dependencies during testing.
MIT License
2.75k stars 100 forks source link

load es6 object #114

Closed hcastellonLan closed 8 years ago

hcastellonLan commented 8 years ago

Hello

i have problems loading the es6 object with proxyquire

i have this class

import BookingClient from "../clients/booking"
class Booking {
  constructor() {
   }
  get(recordLocator, lastName) {
    let bookingClient = new BookingClient();
    return bookingClient.get(recordLocator, lastName);
  }
}
module.exports = {
    Booking: Booking
};

And the dependency

import restify from "restify"
class Booking {
  constructor() {
    }
  get(recordLocator, lastame) {

  }
}
module.exports = {
    Booking: Booking
};

My Test

it("should call a api booking and obtain a booking", (done) =>{

    let bookingStub = {
      get: (recordLocator, lastName) => {
        return "hello"
      }
    }
    let Booking = proxyquire("../../src/controllers/booking", {
      "../clients/booking":bookingStub
    });
    let bookingController = new Booking();
    let x = bookingController.get("AAAAAA", "lastame")
  });

The problem is than i obtain this message "TypeError: object is not a function" in this line " let bookingController = new Booking();

i cheked the path and is ok

Exist a problem using babel with class and objects with proxyquire? or other problem than i can't see?

Thank you very much

bendrucker commented 8 years ago

Please highlight your code in the future.

Throw the class keyword away for now, it's just a distraction here. You started with this:

function Booking () {}

function Booking.get = function get () {}

And then you're trying to stub it with:

{get: function () {}}

Then you try to call bookingStub as a function with new and it fails. Make bookingStub a function and then append your get method.

tuto commented 8 years ago

thank you very much sorry by the highlight

tuto commented 8 years ago

@bendrucker

i tested the solution but for me is fail again

   let bookingStub = () => {};
   bookingStub.get = (recordLocator, lastame) => {}

   let Booking = proxyquire("../../src/controllers/booking", {
      "../clients/booking":bookingStub
    });

    let bookingController = new Booking();

and the result si the same

TypeError: object is not a function

any idea?

bendrucker commented 8 years ago

Booking.Booking would be your constructor since you're nesting under that key