Don't mock domain objects rule?











up vote
3
down vote

favorite












The most experienced developer in my current team has set a few hard rules based on best practices we should follow. Among them is "You never ever mock domain object". I asked him why we couldn't but he never had time to give me a proper answer. Now he's away for a week, and I reached a peak distrust to that rule, here's my situation.



My domain object has an Update method with several parameters, one being an interface for a calculator. It then updates a few fields, runs the calculators and assign its results to some other of its fields.



The proper unit test for the Update method itself is reasonably long.



Now I have some piece of code which do a few things then call Update on such a domain object.



I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself. And I would have to do that everywhere the Update methods gets called.
How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring... I feels this rule is just like shooting myself in the foot..



So I need to know, why You never ever mock domain object"?










share|improve this question


















  • 2




    The main problem is most likely the Update method itself, which attempts to fulfill too many use cases at once. "I would normally mock the objects, and just check the Update method is being called with the proper arguments": that doesn't tell you much on the correctness of the program.
    – plalx
    Nov 19 at 16:31










  • I have a hard time with this correctness definition. For me well unit tested code provides clarity and thrust. The unit tests of Update should show what Update does and proves it does it the right way. If I need to test Update's result everytime it is called, it means I urge the caller to distrust Update's own unit tests. Which would be like stripping away the core concept of unit testing from its essence.
    – Serge Jacquemin
    Nov 20 at 8:58










  • What do you understand under domain object? a domain model? or domain services? a domain model definitely should never be mocked. domain services (like an tax calculator) its pretty much fine, to mock the interface and return the required result
    – Tseng
    Nov 20 at 11:30






  • 1




    @SergeJacquemin Ok, I understand better now. You mean that calling aggregate.Update is part of a larger business process which you are attempting to test, but you feel the need to check the correctness of Update() everytime. I agree, you shouldn't have to do this and you most likely dont either. Why can't you just check the ouputs of the larger process given the inputs, without checking the result of calling Update() specifically. If you are already unit testing Update on it's own that should be enough.
    – plalx
    Nov 20 at 15:01








  • 1




    @SergeJacquemin Well, in that specific scenario it doesn't seem like if there's a need to call a real Update implementation. From the look of things you don't seem to be testing the domain, but testing an application service perhaps? A mock would be fine in this scenario IMO, but if you can't have a mock then just use a helper class to generate a fixture and a fake call to Update() which you can reuse across your tests.
    – plalx
    Nov 20 at 15:42















up vote
3
down vote

favorite












The most experienced developer in my current team has set a few hard rules based on best practices we should follow. Among them is "You never ever mock domain object". I asked him why we couldn't but he never had time to give me a proper answer. Now he's away for a week, and I reached a peak distrust to that rule, here's my situation.



My domain object has an Update method with several parameters, one being an interface for a calculator. It then updates a few fields, runs the calculators and assign its results to some other of its fields.



The proper unit test for the Update method itself is reasonably long.



Now I have some piece of code which do a few things then call Update on such a domain object.



I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself. And I would have to do that everywhere the Update methods gets called.
How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring... I feels this rule is just like shooting myself in the foot..



So I need to know, why You never ever mock domain object"?










share|improve this question


















  • 2




    The main problem is most likely the Update method itself, which attempts to fulfill too many use cases at once. "I would normally mock the objects, and just check the Update method is being called with the proper arguments": that doesn't tell you much on the correctness of the program.
    – plalx
    Nov 19 at 16:31










  • I have a hard time with this correctness definition. For me well unit tested code provides clarity and thrust. The unit tests of Update should show what Update does and proves it does it the right way. If I need to test Update's result everytime it is called, it means I urge the caller to distrust Update's own unit tests. Which would be like stripping away the core concept of unit testing from its essence.
    – Serge Jacquemin
    Nov 20 at 8:58










  • What do you understand under domain object? a domain model? or domain services? a domain model definitely should never be mocked. domain services (like an tax calculator) its pretty much fine, to mock the interface and return the required result
    – Tseng
    Nov 20 at 11:30






  • 1




    @SergeJacquemin Ok, I understand better now. You mean that calling aggregate.Update is part of a larger business process which you are attempting to test, but you feel the need to check the correctness of Update() everytime. I agree, you shouldn't have to do this and you most likely dont either. Why can't you just check the ouputs of the larger process given the inputs, without checking the result of calling Update() specifically. If you are already unit testing Update on it's own that should be enough.
    – plalx
    Nov 20 at 15:01








  • 1




    @SergeJacquemin Well, in that specific scenario it doesn't seem like if there's a need to call a real Update implementation. From the look of things you don't seem to be testing the domain, but testing an application service perhaps? A mock would be fine in this scenario IMO, but if you can't have a mock then just use a helper class to generate a fixture and a fake call to Update() which you can reuse across your tests.
    – plalx
    Nov 20 at 15:42













up vote
3
down vote

favorite









up vote
3
down vote

favorite











The most experienced developer in my current team has set a few hard rules based on best practices we should follow. Among them is "You never ever mock domain object". I asked him why we couldn't but he never had time to give me a proper answer. Now he's away for a week, and I reached a peak distrust to that rule, here's my situation.



My domain object has an Update method with several parameters, one being an interface for a calculator. It then updates a few fields, runs the calculators and assign its results to some other of its fields.



The proper unit test for the Update method itself is reasonably long.



Now I have some piece of code which do a few things then call Update on such a domain object.



I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself. And I would have to do that everywhere the Update methods gets called.
How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring... I feels this rule is just like shooting myself in the foot..



So I need to know, why You never ever mock domain object"?










share|improve this question













The most experienced developer in my current team has set a few hard rules based on best practices we should follow. Among them is "You never ever mock domain object". I asked him why we couldn't but he never had time to give me a proper answer. Now he's away for a week, and I reached a peak distrust to that rule, here's my situation.



My domain object has an Update method with several parameters, one being an interface for a calculator. It then updates a few fields, runs the calculators and assign its results to some other of its fields.



The proper unit test for the Update method itself is reasonably long.



Now I have some piece of code which do a few things then call Update on such a domain object.



I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself. And I would have to do that everywhere the Update methods gets called.
How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring... I feels this rule is just like shooting myself in the foot..



So I need to know, why You never ever mock domain object"?







unit-testing domain-driven-design






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 9:17









Serge Jacquemin

304




304








  • 2




    The main problem is most likely the Update method itself, which attempts to fulfill too many use cases at once. "I would normally mock the objects, and just check the Update method is being called with the proper arguments": that doesn't tell you much on the correctness of the program.
    – plalx
    Nov 19 at 16:31










  • I have a hard time with this correctness definition. For me well unit tested code provides clarity and thrust. The unit tests of Update should show what Update does and proves it does it the right way. If I need to test Update's result everytime it is called, it means I urge the caller to distrust Update's own unit tests. Which would be like stripping away the core concept of unit testing from its essence.
    – Serge Jacquemin
    Nov 20 at 8:58










  • What do you understand under domain object? a domain model? or domain services? a domain model definitely should never be mocked. domain services (like an tax calculator) its pretty much fine, to mock the interface and return the required result
    – Tseng
    Nov 20 at 11:30






  • 1




    @SergeJacquemin Ok, I understand better now. You mean that calling aggregate.Update is part of a larger business process which you are attempting to test, but you feel the need to check the correctness of Update() everytime. I agree, you shouldn't have to do this and you most likely dont either. Why can't you just check the ouputs of the larger process given the inputs, without checking the result of calling Update() specifically. If you are already unit testing Update on it's own that should be enough.
    – plalx
    Nov 20 at 15:01








  • 1




    @SergeJacquemin Well, in that specific scenario it doesn't seem like if there's a need to call a real Update implementation. From the look of things you don't seem to be testing the domain, but testing an application service perhaps? A mock would be fine in this scenario IMO, but if you can't have a mock then just use a helper class to generate a fixture and a fake call to Update() which you can reuse across your tests.
    – plalx
    Nov 20 at 15:42














  • 2




    The main problem is most likely the Update method itself, which attempts to fulfill too many use cases at once. "I would normally mock the objects, and just check the Update method is being called with the proper arguments": that doesn't tell you much on the correctness of the program.
    – plalx
    Nov 19 at 16:31










  • I have a hard time with this correctness definition. For me well unit tested code provides clarity and thrust. The unit tests of Update should show what Update does and proves it does it the right way. If I need to test Update's result everytime it is called, it means I urge the caller to distrust Update's own unit tests. Which would be like stripping away the core concept of unit testing from its essence.
    – Serge Jacquemin
    Nov 20 at 8:58










  • What do you understand under domain object? a domain model? or domain services? a domain model definitely should never be mocked. domain services (like an tax calculator) its pretty much fine, to mock the interface and return the required result
    – Tseng
    Nov 20 at 11:30






  • 1




    @SergeJacquemin Ok, I understand better now. You mean that calling aggregate.Update is part of a larger business process which you are attempting to test, but you feel the need to check the correctness of Update() everytime. I agree, you shouldn't have to do this and you most likely dont either. Why can't you just check the ouputs of the larger process given the inputs, without checking the result of calling Update() specifically. If you are already unit testing Update on it's own that should be enough.
    – plalx
    Nov 20 at 15:01








  • 1




    @SergeJacquemin Well, in that specific scenario it doesn't seem like if there's a need to call a real Update implementation. From the look of things you don't seem to be testing the domain, but testing an application service perhaps? A mock would be fine in this scenario IMO, but if you can't have a mock then just use a helper class to generate a fixture and a fake call to Update() which you can reuse across your tests.
    – plalx
    Nov 20 at 15:42








2




2




The main problem is most likely the Update method itself, which attempts to fulfill too many use cases at once. "I would normally mock the objects, and just check the Update method is being called with the proper arguments": that doesn't tell you much on the correctness of the program.
– plalx
Nov 19 at 16:31




The main problem is most likely the Update method itself, which attempts to fulfill too many use cases at once. "I would normally mock the objects, and just check the Update method is being called with the proper arguments": that doesn't tell you much on the correctness of the program.
– plalx
Nov 19 at 16:31












I have a hard time with this correctness definition. For me well unit tested code provides clarity and thrust. The unit tests of Update should show what Update does and proves it does it the right way. If I need to test Update's result everytime it is called, it means I urge the caller to distrust Update's own unit tests. Which would be like stripping away the core concept of unit testing from its essence.
– Serge Jacquemin
Nov 20 at 8:58




I have a hard time with this correctness definition. For me well unit tested code provides clarity and thrust. The unit tests of Update should show what Update does and proves it does it the right way. If I need to test Update's result everytime it is called, it means I urge the caller to distrust Update's own unit tests. Which would be like stripping away the core concept of unit testing from its essence.
– Serge Jacquemin
Nov 20 at 8:58












What do you understand under domain object? a domain model? or domain services? a domain model definitely should never be mocked. domain services (like an tax calculator) its pretty much fine, to mock the interface and return the required result
– Tseng
Nov 20 at 11:30




What do you understand under domain object? a domain model? or domain services? a domain model definitely should never be mocked. domain services (like an tax calculator) its pretty much fine, to mock the interface and return the required result
– Tseng
Nov 20 at 11:30




1




1




@SergeJacquemin Ok, I understand better now. You mean that calling aggregate.Update is part of a larger business process which you are attempting to test, but you feel the need to check the correctness of Update() everytime. I agree, you shouldn't have to do this and you most likely dont either. Why can't you just check the ouputs of the larger process given the inputs, without checking the result of calling Update() specifically. If you are already unit testing Update on it's own that should be enough.
– plalx
Nov 20 at 15:01






@SergeJacquemin Ok, I understand better now. You mean that calling aggregate.Update is part of a larger business process which you are attempting to test, but you feel the need to check the correctness of Update() everytime. I agree, you shouldn't have to do this and you most likely dont either. Why can't you just check the ouputs of the larger process given the inputs, without checking the result of calling Update() specifically. If you are already unit testing Update on it's own that should be enough.
– plalx
Nov 20 at 15:01






1




1




@SergeJacquemin Well, in that specific scenario it doesn't seem like if there's a need to call a real Update implementation. From the look of things you don't seem to be testing the domain, but testing an application service perhaps? A mock would be fine in this scenario IMO, but if you can't have a mock then just use a helper class to generate a fixture and a fake call to Update() which you can reuse across your tests.
– plalx
Nov 20 at 15:42




@SergeJacquemin Well, in that specific scenario it doesn't seem like if there's a need to call a real Update implementation. From the look of things you don't seem to be testing the domain, but testing an application service perhaps? A mock would be fine in this scenario IMO, but if you can't have a mock then just use a helper class to generate a fixture and a fake call to Update() which you can reuse across your tests.
– plalx
Nov 20 at 15:42












3 Answers
3






active

oldest

votes

















up vote
2
down vote














You never ever mock domain object




This is more of a heuristic than a best practice. And I can imagine that it make sense in some projects.



This heuristic can help to have better tests. Good tests are easy to write, easy to read and they will break as soon as something goes wrong. If you mock your domain model, you will probably also have to mock it's behavior. And if you have some complex behavior in your model, mocking it will be time consuming. The test will also be more fragile (mistakes can happen in predicting the domain model output).



The other alternative is to do sociable unit testing. That means the unit to be tested will not be a single class in isolation but a class and some of its dependencies. In other words, you just use a concrete object of your domain model in the test.




I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.




Checking that the update method is being called doesn't necessarily mean the success of the test. This is probably not what you want to check to see if your service works correctly. If you pass a concrete class of your model(including the calculators) you won't have to do any useless checks or repeat what has been done in other tests.



One of the differences between this solution and testing classes in isolation is that when there is bug in your domain model, many other tests will fail. But this is totally fine in my opinion since it will have to be fixed anyway.






share|improve this answer




























    up vote
    2
    down vote














    You never ever mock domain object




    There might be different reasons to avoid mocking your domain objects:




    1. Mocking libraries, the same as ORMs, can affect design of your domain objects. They usually require interfaces or virtual methods in C#.


    2. A preference to test against real instances. Very often people use mocking libraries because it is an easy way to create a fake or a stub. Very often you can build an instance of your domain object and use it in your test. Most likely a fact that method Update was called can be easily checked via state change.




    I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.



    How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring...




    It was already mentioned in the comment that probably Update method has to many responsibilities and that's why is used in lots of use cases.



    There are different tastes when it comes to defining a system under test (sut). If you tend to test classes then you often find yourself using mocking libraries. If you tend to test scenarios or behaviors then most likely you will only care about state changes.



    You can probably explore a different design for your domain object: Try to split Update method into explicit domain specific methods so each client will trigger a different behavior. Given this approach a "reasonably long" test for the Update method can even become obsolete since it will be tested by other tests.






    share|improve this answer




























      up vote
      1
      down vote













      While Update() is probably not as tailored a method as you would expect from a rich domain model - which may have an impact on test fragility - I agree that you should take "never do X" advice with a grain of salt.



      If you understand all the implications and know that the integration test version will be less maintainable, definitely use mocks. You, and not blanket statements or cargo cults, are the ultimate judge to what's better for your system in your context.






      share|improve this answer





















        Your Answer






        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: "1"
        };
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fstackoverflow.com%2fquestions%2f53371495%2fdont-mock-domain-objects-rule%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote














        You never ever mock domain object




        This is more of a heuristic than a best practice. And I can imagine that it make sense in some projects.



        This heuristic can help to have better tests. Good tests are easy to write, easy to read and they will break as soon as something goes wrong. If you mock your domain model, you will probably also have to mock it's behavior. And if you have some complex behavior in your model, mocking it will be time consuming. The test will also be more fragile (mistakes can happen in predicting the domain model output).



        The other alternative is to do sociable unit testing. That means the unit to be tested will not be a single class in isolation but a class and some of its dependencies. In other words, you just use a concrete object of your domain model in the test.




        I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.




        Checking that the update method is being called doesn't necessarily mean the success of the test. This is probably not what you want to check to see if your service works correctly. If you pass a concrete class of your model(including the calculators) you won't have to do any useless checks or repeat what has been done in other tests.



        One of the differences between this solution and testing classes in isolation is that when there is bug in your domain model, many other tests will fail. But this is totally fine in my opinion since it will have to be fixed anyway.






        share|improve this answer

























          up vote
          2
          down vote














          You never ever mock domain object




          This is more of a heuristic than a best practice. And I can imagine that it make sense in some projects.



          This heuristic can help to have better tests. Good tests are easy to write, easy to read and they will break as soon as something goes wrong. If you mock your domain model, you will probably also have to mock it's behavior. And if you have some complex behavior in your model, mocking it will be time consuming. The test will also be more fragile (mistakes can happen in predicting the domain model output).



          The other alternative is to do sociable unit testing. That means the unit to be tested will not be a single class in isolation but a class and some of its dependencies. In other words, you just use a concrete object of your domain model in the test.




          I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.




          Checking that the update method is being called doesn't necessarily mean the success of the test. This is probably not what you want to check to see if your service works correctly. If you pass a concrete class of your model(including the calculators) you won't have to do any useless checks or repeat what has been done in other tests.



          One of the differences between this solution and testing classes in isolation is that when there is bug in your domain model, many other tests will fail. But this is totally fine in my opinion since it will have to be fixed anyway.






          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote










            You never ever mock domain object




            This is more of a heuristic than a best practice. And I can imagine that it make sense in some projects.



            This heuristic can help to have better tests. Good tests are easy to write, easy to read and they will break as soon as something goes wrong. If you mock your domain model, you will probably also have to mock it's behavior. And if you have some complex behavior in your model, mocking it will be time consuming. The test will also be more fragile (mistakes can happen in predicting the domain model output).



            The other alternative is to do sociable unit testing. That means the unit to be tested will not be a single class in isolation but a class and some of its dependencies. In other words, you just use a concrete object of your domain model in the test.




            I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.




            Checking that the update method is being called doesn't necessarily mean the success of the test. This is probably not what you want to check to see if your service works correctly. If you pass a concrete class of your model(including the calculators) you won't have to do any useless checks or repeat what has been done in other tests.



            One of the differences between this solution and testing classes in isolation is that when there is bug in your domain model, many other tests will fail. But this is totally fine in my opinion since it will have to be fixed anyway.






            share|improve this answer













            You never ever mock domain object




            This is more of a heuristic than a best practice. And I can imagine that it make sense in some projects.



            This heuristic can help to have better tests. Good tests are easy to write, easy to read and they will break as soon as something goes wrong. If you mock your domain model, you will probably also have to mock it's behavior. And if you have some complex behavior in your model, mocking it will be time consuming. The test will also be more fragile (mistakes can happen in predicting the domain model output).



            The other alternative is to do sociable unit testing. That means the unit to be tested will not be a single class in isolation but a class and some of its dependencies. In other words, you just use a concrete object of your domain model in the test.




            I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.




            Checking that the update method is being called doesn't necessarily mean the success of the test. This is probably not what you want to check to see if your service works correctly. If you pass a concrete class of your model(including the calculators) you won't have to do any useless checks or repeat what has been done in other tests.



            One of the differences between this solution and testing classes in isolation is that when there is bug in your domain model, many other tests will fail. But this is totally fine in my opinion since it will have to be fixed anyway.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 at 12:31









            Mohamed Bouallegue

            8881016




            8881016
























                up vote
                2
                down vote














                You never ever mock domain object




                There might be different reasons to avoid mocking your domain objects:




                1. Mocking libraries, the same as ORMs, can affect design of your domain objects. They usually require interfaces or virtual methods in C#.


                2. A preference to test against real instances. Very often people use mocking libraries because it is an easy way to create a fake or a stub. Very often you can build an instance of your domain object and use it in your test. Most likely a fact that method Update was called can be easily checked via state change.




                I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.



                How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring...




                It was already mentioned in the comment that probably Update method has to many responsibilities and that's why is used in lots of use cases.



                There are different tastes when it comes to defining a system under test (sut). If you tend to test classes then you often find yourself using mocking libraries. If you tend to test scenarios or behaviors then most likely you will only care about state changes.



                You can probably explore a different design for your domain object: Try to split Update method into explicit domain specific methods so each client will trigger a different behavior. Given this approach a "reasonably long" test for the Update method can even become obsolete since it will be tested by other tests.






                share|improve this answer

























                  up vote
                  2
                  down vote














                  You never ever mock domain object




                  There might be different reasons to avoid mocking your domain objects:




                  1. Mocking libraries, the same as ORMs, can affect design of your domain objects. They usually require interfaces or virtual methods in C#.


                  2. A preference to test against real instances. Very often people use mocking libraries because it is an easy way to create a fake or a stub. Very often you can build an instance of your domain object and use it in your test. Most likely a fact that method Update was called can be easily checked via state change.




                  I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.



                  How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring...




                  It was already mentioned in the comment that probably Update method has to many responsibilities and that's why is used in lots of use cases.



                  There are different tastes when it comes to defining a system under test (sut). If you tend to test classes then you often find yourself using mocking libraries. If you tend to test scenarios or behaviors then most likely you will only care about state changes.



                  You can probably explore a different design for your domain object: Try to split Update method into explicit domain specific methods so each client will trigger a different behavior. Given this approach a "reasonably long" test for the Update method can even become obsolete since it will be tested by other tests.






                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote










                    You never ever mock domain object




                    There might be different reasons to avoid mocking your domain objects:




                    1. Mocking libraries, the same as ORMs, can affect design of your domain objects. They usually require interfaces or virtual methods in C#.


                    2. A preference to test against real instances. Very often people use mocking libraries because it is an easy way to create a fake or a stub. Very often you can build an instance of your domain object and use it in your test. Most likely a fact that method Update was called can be easily checked via state change.




                    I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.



                    How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring...




                    It was already mentioned in the comment that probably Update method has to many responsibilities and that's why is used in lots of use cases.



                    There are different tastes when it comes to defining a system under test (sut). If you tend to test classes then you often find yourself using mocking libraries. If you tend to test scenarios or behaviors then most likely you will only care about state changes.



                    You can probably explore a different design for your domain object: Try to split Update method into explicit domain specific methods so each client will trigger a different behavior. Given this approach a "reasonably long" test for the Update method can even become obsolete since it will be tested by other tests.






                    share|improve this answer













                    You never ever mock domain object




                    There might be different reasons to avoid mocking your domain objects:




                    1. Mocking libraries, the same as ORMs, can affect design of your domain objects. They usually require interfaces or virtual methods in C#.


                    2. A preference to test against real instances. Very often people use mocking libraries because it is an easy way to create a fake or a stub. Very often you can build an instance of your domain object and use it in your test. Most likely a fact that method Update was called can be easily checked via state change.




                    I would normally mock the objects, and just check the Update method is being called with the proper arguments. But now, I have to test it's being properly called, by checking its fields and mocking the calculator then same way I did when unit testing the Update method itself.



                    How fun will it be when the Update method change a bit, and every of those tests suddenly break and need refactoring...




                    It was already mentioned in the comment that probably Update method has to many responsibilities and that's why is used in lots of use cases.



                    There are different tastes when it comes to defining a system under test (sut). If you tend to test classes then you often find yourself using mocking libraries. If you tend to test scenarios or behaviors then most likely you will only care about state changes.



                    You can probably explore a different design for your domain object: Try to split Update method into explicit domain specific methods so each client will trigger a different behavior. Given this approach a "reasonably long" test for the Update method can even become obsolete since it will be tested by other tests.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 at 20:14









                    Ilya Palkin

                    7,17421834




                    7,17421834






















                        up vote
                        1
                        down vote













                        While Update() is probably not as tailored a method as you would expect from a rich domain model - which may have an impact on test fragility - I agree that you should take "never do X" advice with a grain of salt.



                        If you understand all the implications and know that the integration test version will be less maintainable, definitely use mocks. You, and not blanket statements or cargo cults, are the ultimate judge to what's better for your system in your context.






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          While Update() is probably not as tailored a method as you would expect from a rich domain model - which may have an impact on test fragility - I agree that you should take "never do X" advice with a grain of salt.



                          If you understand all the implications and know that the integration test version will be less maintainable, definitely use mocks. You, and not blanket statements or cargo cults, are the ultimate judge to what's better for your system in your context.






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            While Update() is probably not as tailored a method as you would expect from a rich domain model - which may have an impact on test fragility - I agree that you should take "never do X" advice with a grain of salt.



                            If you understand all the implications and know that the integration test version will be less maintainable, definitely use mocks. You, and not blanket statements or cargo cults, are the ultimate judge to what's better for your system in your context.






                            share|improve this answer












                            While Update() is probably not as tailored a method as you would expect from a rich domain model - which may have an impact on test fragility - I agree that you should take "never do X" advice with a grain of salt.



                            If you understand all the implications and know that the integration test version will be less maintainable, definitely use mocks. You, and not blanket statements or cargo cults, are the ultimate judge to what's better for your system in your context.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 at 12:31









                            guillaume31

                            11.1k2339




                            11.1k2339






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • 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.





                                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%2fstackoverflow.com%2fquestions%2f53371495%2fdont-mock-domain-objects-rule%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

                                "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                Alcedinidae

                                Origin of the phrase “under your belt”?