Javascript Unit testing dependencies - How to make a test fail when a depended object changes
-
21-06-2021 - |
Pregunta
I have a controller which is depended on a model property. I wrote a test which test this controller and stubbed the model, How can I make my controller test fail whenever the model property name changes?
Here is an example:
Model:
function Model(obj){
this.id = obj.id;
this.name = obj.name;
}
Controller:
function Controller( model ){
this.model = model
};
Controller.prototype.showName = function() {
return this.model.name;
}
Controller test:
describe("A controller", function(){
var controller = new Controller( new Model({id:1, name: "john"}) );
it ("should show the person name", function(){
expect(controller.showName).toEqual("john");
});
});
I want this test to fail whenever the "name" property changes to something else, something like "name2".
Anyone got a good suggestion on how to do it in javascript?
Thanks, Shai
Solución
Since the model is exposed you can check for the existence of the property.
expect(controller.name).toBeDefined();
On the other hand, this test would fail anyway if the property of the model was renamed and the controller wouldn't be altered too.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow