Test-Spy Implementation
up vote
1
down vote
favorite
I have tried to write a test-spy myself. Just for getting more familar with the topic.
Here's the code:
// --------- SPY - Start -----------------------------------
class Spy {
constructor(func) {
this.func = func;
this.returnValue = null;
this.result = null;
this.countFuncCalled = 0;
}
invoke(...givenArgs) {
this.receivedArgs = givenArgs;
this.returnValue = this.func(...givenArgs);
this.countFuncCalled++;
}
}
// --------- SPY - End ------------------------------------
const calc = {
add: (a, b) => {
return a + b;
},
sub: (a, b) => {
return a - b;
}
}
const addSpy = new Spy(calc.sub);
addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
What do you think about my implementation? Is is done in a basically correct way?
I'm I using the ES6-features (Classes, Rest, Spread) right?
What would you have done differently and why?
Looking forward to reading your comments and answers.
javascript unit-testing ecmascript-6
add a comment |
up vote
1
down vote
favorite
I have tried to write a test-spy myself. Just for getting more familar with the topic.
Here's the code:
// --------- SPY - Start -----------------------------------
class Spy {
constructor(func) {
this.func = func;
this.returnValue = null;
this.result = null;
this.countFuncCalled = 0;
}
invoke(...givenArgs) {
this.receivedArgs = givenArgs;
this.returnValue = this.func(...givenArgs);
this.countFuncCalled++;
}
}
// --------- SPY - End ------------------------------------
const calc = {
add: (a, b) => {
return a + b;
},
sub: (a, b) => {
return a - b;
}
}
const addSpy = new Spy(calc.sub);
addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
What do you think about my implementation? Is is done in a basically correct way?
I'm I using the ES6-features (Classes, Rest, Spread) right?
What would you have done differently and why?
Looking forward to reading your comments and answers.
javascript unit-testing ecmascript-6
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have tried to write a test-spy myself. Just for getting more familar with the topic.
Here's the code:
// --------- SPY - Start -----------------------------------
class Spy {
constructor(func) {
this.func = func;
this.returnValue = null;
this.result = null;
this.countFuncCalled = 0;
}
invoke(...givenArgs) {
this.receivedArgs = givenArgs;
this.returnValue = this.func(...givenArgs);
this.countFuncCalled++;
}
}
// --------- SPY - End ------------------------------------
const calc = {
add: (a, b) => {
return a + b;
},
sub: (a, b) => {
return a - b;
}
}
const addSpy = new Spy(calc.sub);
addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
What do you think about my implementation? Is is done in a basically correct way?
I'm I using the ES6-features (Classes, Rest, Spread) right?
What would you have done differently and why?
Looking forward to reading your comments and answers.
javascript unit-testing ecmascript-6
I have tried to write a test-spy myself. Just for getting more familar with the topic.
Here's the code:
// --------- SPY - Start -----------------------------------
class Spy {
constructor(func) {
this.func = func;
this.returnValue = null;
this.result = null;
this.countFuncCalled = 0;
}
invoke(...givenArgs) {
this.receivedArgs = givenArgs;
this.returnValue = this.func(...givenArgs);
this.countFuncCalled++;
}
}
// --------- SPY - End ------------------------------------
const calc = {
add: (a, b) => {
return a + b;
},
sub: (a, b) => {
return a - b;
}
}
const addSpy = new Spy(calc.sub);
addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
What do you think about my implementation? Is is done in a basically correct way?
I'm I using the ES6-features (Classes, Rest, Spread) right?
What would you have done differently and why?
Looking forward to reading your comments and answers.
// --------- SPY - Start -----------------------------------
class Spy {
constructor(func) {
this.func = func;
this.returnValue = null;
this.result = null;
this.countFuncCalled = 0;
}
invoke(...givenArgs) {
this.receivedArgs = givenArgs;
this.returnValue = this.func(...givenArgs);
this.countFuncCalled++;
}
}
// --------- SPY - End ------------------------------------
const calc = {
add: (a, b) => {
return a + b;
},
sub: (a, b) => {
return a - b;
}
}
const addSpy = new Spy(calc.sub);
addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
// --------- SPY - Start -----------------------------------
class Spy {
constructor(func) {
this.func = func;
this.returnValue = null;
this.result = null;
this.countFuncCalled = 0;
}
invoke(...givenArgs) {
this.receivedArgs = givenArgs;
this.returnValue = this.func(...givenArgs);
this.countFuncCalled++;
}
}
// --------- SPY - End ------------------------------------
const calc = {
add: (a, b) => {
return a + b;
},
sub: (a, b) => {
return a - b;
}
}
const addSpy = new Spy(calc.sub);
addSpy.invoke(9, 4);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
addSpy.invoke(8, 7);
console.log(`Used arguments: ${addSpy.receivedArgs.join(", ")}`);
console.log(`Return value: ${addSpy.returnValue}`);
console.log(`Count of function-calls: ${addSpy.countFuncCalled}`);
javascript unit-testing ecmascript-6
javascript unit-testing ecmascript-6
edited 2 days ago
Sᴀᴍ Onᴇᴌᴀ
7,88561750
7,88561750
asked Oct 24 at 13:23
michael.zech
1,6921432
1,6921432
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
What do you think about my implementation? Is is done in a basically correct way?
Yeah who is to say what is "correct"? I mostly have worked with the chaiJS spies API and it feels like a basic version of that library.
I'm I using the ES6-features (Classes, Rest, Spread) right?
While there isn't much to the code, I would say it looks like those features are used right. You could consider using more class features like getters and setters
In the constructor, there exists this line:
this.result = null;
but the result
property doesn't appear to be used anywhere. What is that for?
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
What do you think about my implementation? Is is done in a basically correct way?
Yeah who is to say what is "correct"? I mostly have worked with the chaiJS spies API and it feels like a basic version of that library.
I'm I using the ES6-features (Classes, Rest, Spread) right?
While there isn't much to the code, I would say it looks like those features are used right. You could consider using more class features like getters and setters
In the constructor, there exists this line:
this.result = null;
but the result
property doesn't appear to be used anywhere. What is that for?
add a comment |
up vote
0
down vote
What do you think about my implementation? Is is done in a basically correct way?
Yeah who is to say what is "correct"? I mostly have worked with the chaiJS spies API and it feels like a basic version of that library.
I'm I using the ES6-features (Classes, Rest, Spread) right?
While there isn't much to the code, I would say it looks like those features are used right. You could consider using more class features like getters and setters
In the constructor, there exists this line:
this.result = null;
but the result
property doesn't appear to be used anywhere. What is that for?
add a comment |
up vote
0
down vote
up vote
0
down vote
What do you think about my implementation? Is is done in a basically correct way?
Yeah who is to say what is "correct"? I mostly have worked with the chaiJS spies API and it feels like a basic version of that library.
I'm I using the ES6-features (Classes, Rest, Spread) right?
While there isn't much to the code, I would say it looks like those features are used right. You could consider using more class features like getters and setters
In the constructor, there exists this line:
this.result = null;
but the result
property doesn't appear to be used anywhere. What is that for?
What do you think about my implementation? Is is done in a basically correct way?
Yeah who is to say what is "correct"? I mostly have worked with the chaiJS spies API and it feels like a basic version of that library.
I'm I using the ES6-features (Classes, Rest, Spread) right?
While there isn't much to the code, I would say it looks like those features are used right. You could consider using more class features like getters and setters
In the constructor, there exists this line:
this.result = null;
but the result
property doesn't appear to be used anywhere. What is that for?
answered 2 days ago
Sᴀᴍ Onᴇᴌᴀ
7,88561750
7,88561750
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206188%2ftest-spy-implementation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown