Test-Spy Implementation











up vote
1
down vote

favorite
1












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.










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    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.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Sᴀᴍ Onᴇᴌᴀ

      7,88561750




      7,88561750










      asked Oct 24 at 13:23









      michael.zech

      1,6921432




      1,6921432






















          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?






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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?






            share|improve this answer

























              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?






              share|improve this answer























                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?






                share|improve this answer













                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?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Sᴀᴍ Onᴇᴌᴀ

                7,88561750




                7,88561750






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Morgemoulin

                    Scott Moir

                    Souastre