角度单位测试困惑

时间:2015-06-11 20:52:59

标签: angularjs jasmine karma-jasmine

我正在测试我的控制器。我一直在

    Error: Expected POST /auth/signup with different data
EXPECTED: {"username":"justin","firstName":"Justin","lastName":"Young","email":"xxxx@xxx.com","company":"5579d602ba9f26a414be5d57","url":"http://www.me.com","referrer":"me@me.com"}

它按照预期完成了auth / signup的帖子,但数据是空的?我将sampleUserResponse传递给了期望值,因此我不明白为什么它没有将这些数据作为响应传回去。我究竟做错了什么?

我的测试:

'use strict';

(function() {
// Authentication controller Spec
describe('AdminItemController', function() {
    // Initialize global variables
    var controller,
        scope,
        $httpBackend,
        $stateParams,
        $location;

    beforeEach(function() {
        jasmine.addMatchers({
            toEqualData: function(util, customEqualityTesters) {
                return {
                    compare: function(actual, expected) {
                        return {
                            pass: angular.equals(actual, expected)
                        };
                    }
                };
            }
        });
    });

    // Load the main application module
    beforeEach(module(ApplicationConfiguration.applicationModuleName));

    // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
    // This allows us to inject a service but then attach it to a variable
    // with the same name as the service.
    beforeEach(inject(function($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_) {
        // Set a new global scope
        scope = $rootScope.$new();
        // Point global variables to injected services
        $stateParams = _$stateParams_;
        $httpBackend = _$httpBackend_;
        $location = _$location_;
        // Initialize the Authentication controller
        controller = $controller('AdminItemController', {
            $scope: scope,
            $stateParams:$stateParams
        });

        $httpBackend.when('GET', 'modules/core/views/home.client.view.html').respond({});
    }));

    it('$scope.create() with valid form data should send a POST request with the form input values and then locate to new object URL', inject(function(Users) {
        // Create a sample article object
        var sampleUserPostData = new Users({
            username: 'justin',
            firstName:'Justin',
            lastName:'Young',
            email:'xxxx@xxx.com',
            company:'5579d602ba9f26a414be5d57',
            url:'http://www.me.com',
            referrer:'me@me.com'
        });

        // Create a sample User response
        var sampleUserResponse = new Users({
            _id:'4579d602ba9f26a414be5d59',
            username: 'justin',
            firstName:'Justin',
            lastName:'Young',
            email:'xxxx@xxx.com',
            company:'5579d602ba9f26a414be5d57',
            url:'http://www.me.com',
            referrer:'me@me.com'
        });

        // Fixture mock form input values
        //scope.title = 'An User about MEAN';
        //scope.content = 'MEAN rocks!';

        // Set POST response
        $httpBackend.expectPOST('/auth/signup', sampleUserPostData).respond(sampleUserResponse);

        // Run controller functionality
        scope.addPost();
        $httpBackend.flush();

        // Test form inputs are reset
        //expect(scope.title).toEqual('');
        //expect(scope.content).toEqual('');

        // Test URL redirection after the User was created
        //expect($location.path()).toBe('/admin/users/' + sampleUserResponse._id);
    }));

});
  }());

我的简化控制器:

.controller('AdminItemController', ['$scope', '$http', '$location','apiResource','$stateParams', '$state','$log',
function($scope, $http, $location, apiResource, $stateParams, $state, $log) {

    $scope.addPost = function() {

        apiResource.save({api_resource:'auth', api_action: 'signup'},$scope.item).$promise.then(function(response){
          $scope.$parent.users.push($scope.item);
      });
    };
}
])

1 个答案:

答案 0 :(得分:0)

问题出在您的控制器中,您使用params发送的POST$scope.item,但在测试中,您不要设置您的{{ 1}}成为任何东西。因此,将发送$scope.item POST {因为undefined params$scope.item)。此外,在您的测试中,您期望发送的undefined等于params。显然它会失败,因为sampleUserPostData!== undefined。您可以做的只是在sampleUserPostData之前设置scope.item = sampleUserPostData;,这样就可以了。

Working fiddle