How to mock ng-grid when unit testing a controller

前端 未结 2 441
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 14:30

I\'m currently trying to write tests for existing blocks of code and running into an issue with a controller that has a nested ng-grid inside of it. The issue comes from the con

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-10 15:03

    Your (major!) problem here is that the controller is making assumptions about a View. It should not know about and thus not interact with ng-grid. Controllers should be View-independent! That quality (and Dependency Injection) is what makes controllers highly testable. The controller should only change the ViewModel (i.e. its $scope), and in testing you validate that the ViewModel is correct.

    Doing otherwise goes against the MVVM paradigm and best practices.

    If you feel like you must access the View (i.e. directives, DOM elements, etc...) from the controller, you are likely doing something wrong.

提交回复
热议问题