-
Notifications
You must be signed in to change notification settings - Fork 142
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
Model instance creation assistance #149
Comments
Another try:
But with no success, I'm getting now
So I'm not mocking the object properly. I'm going to keep trying and post my development if I can make it work. I hope it's ok for me to post here since I'm new with testing. |
My approach was wrong, and I switched to dependency injection and some tests are now going through, but the problem still exists with the contructor. My function is now a little different:
And I'm able to mock different REST controller function except this one. I'm trying:
With no success as the console shows: 1) UserController createUser without any errors:
TypeError: _testdouble2.default.when(...).return is not a function
at Context.<anonymous> (__test__/unit/controllers/user.test.js:91:7) |
My
But since I'm not sure how to mock the constructor function, I'm still reading about this. |
I solve this problem creating a mockModel = td.function()
mockModel.findById = td.function();
mockModel.findByIdAndUpdate = td.function();
mockModel.findByIdAndRemove = td.function();
mockModel.save = td.function();
controller = new UserController({ User: mockModel }); This way at least I have the certainty that the controller is calling those model functions correctly. The final piece is to also create a Now the test can occur: it('without any errors', (done) => {
let userBody = {
name: 'test-name',
username: 'test-username',
email: 'test-email'
};
let mockRequest = { body: userBody };
var mockResponse = {
send: td.function()
};
// New User created from mockModel
let mockUser = {
save: td.function()
}
td.when(controller.User(userBody))
.thenReturn(Promise.resolve(mockUser));
td.when(mockUser.save())
.thenReturn(Promise.resolve(userBody))
td.when(mockResponse.send(userBody))
.thenDo(() => done());
controller.createUser(mockRequest, mockResponse);
}); I'm learning about this as I go, I hope it is not spamming the team developing this tool. |
Hello, I'm giving a try to td and so far I really enjoy it! But I've encountered a halt when I'm trying to create an instance of an mongoose object. I want to test the
userCreate
and for that I've tried several approaches and all have failed.The function to be tested:
So my initial approach is:
But
done()
is never called, so my next try involves trying to replace the class with no success. I found an Issue regarding constructors, but haven't been able to work this out. Is there a better way to approach objects as such?The text was updated successfully, but these errors were encountered: