1

我最近开始使用 AngularJS MVC 框架。我以前在 GWT 工作。不,这是我注意到使用 AngularJS 的问题。当我开始实现一个新的控制器验收测试套件时,我会尽可能多地练习 TDD,然后在编写第一个验收测试之后,您开始编写单元测试并逐步实现您的控制器。问题是,我看到它,您的对象字段在单元测试、E2E 测试、控制器和视图中的至少 4 个位置是否应该相同。这是追逐的噩梦。我将举一个简单的例子来说明我的意思。我希望我错了,我的工作方法是错误的,如果是这样,我希望有人能纠正我。

// in controller 

     $scope.newUsers = new Array(); // <-- $scope.newUsers should be the same array In unit tests.

        $scope.addUserTobeCreated = function (user) {// <-- functions should be added in the view too
            $scope.newUsers.push(user);
        };

        $scope.createUsers = function () {
            $http.post("some url", $scope.newUsers).success(function () {
              // do something 
            });
        };

// in view 
  <label class="form-input-label">name:</label>
  <input type="text" size="15" maxlength="15" class="form-input-field  ipInput"  ng-model="user.name"> 
<label class="form-input-label">age:</label>
<input type="text" size="15" maxlength="15" class="form-input-field  ipInput"  ng-model="user.age">
<label class="form-input-label">position:</label>
<input type="text" size="15" maxlength="15" class="form-input-field  ipInput"  ng-model="user.position">
   <ul>
        <li ng-repeat="user in newUsers">
            Name:{{user.name}}, Age:{{user.age}}

        </li>
    </ul>

// in unit test

  it("should add user to new users array to be added", function () {
        var user = {name:"ivan"};
        scope.addUserToBeCreated(user);
        expect(scope.newUsers.length).toBe(1); // <-- we have "newUsers" in 4 palces until now. Controller, unit tests, and view 2 times ! . 
        // do some assertions ... 
    });

如果我想更改 newUsers 数组名称,我必须在 4 个地方进行!

此外,考虑将对象传递给接受 JSON 对象的服务器。在服务器上,我必须将 JSON 映射到我的服务器对象。使用像 sitebircs 这样的框架很容易做到这一点。但是,为了能够反序列化来自客户端的对象,必须再次保持类名一致。我是唯一一个看到这个问题的人吗?如果我弄错了,请纠正我。或者只有我真的有这个问题。如果是这种情况,请指导我使用 JS 框架的正确方法。
感谢您的回复。

4

1 回答 1

2

If you use a good Javascript IDE the refactoring support will let you do it in one go.

For example, in IDEA I can put my cursor on newUsers and press Shift-F6. Then check "Search in comments and strings" to also pick up the string in the html.

This problem exists also with GWT, but then you probably didn't notice because you were comfortable with refactoring support in your Java IDE.

于 2013-01-19T07:20:33.367 回答