问题
I am rather new for software testing. I wonder what is the right way to make a unit testing for observer pattern? e.g. if we have the following 4 classes implemented: Subject class(attach/detach/notify), Observer interface class (update virtual method), ConcreteSubject class, and ConcreteObserver (concrete update method). how to write test tracks for each of these classes? Is there any good example to take a reference?
Thanks for any inputs.
回答1:
For the Subject, you can test: if the observer is well attach(count the number of observers and check if the one attach is the good one), and after if the observer is well detach.
You can also check by adding more than one observer just to be sure your collection of Observers is well implemented.
You can check the notification by creating a mock observer that contains a boolean set to true if he is updated.
来源:https://stackoverflow.com/questions/4713757/unit-testing-for-observer-pattern