I've read pretty much every other question on here about this issue and it seems like people have the same issue but the answer doesn't fix my problem. I have a module that is my main app module, I have a controller module that houses my controller. I also have a services module that houses a resource I created for retrieving data.
When I run the code it works perfectly, but when I try to run my unit test and it goes to inject the service into the controller, I get the unknown provider error. I've tried including the services module as a dependency inside the controller module but still no luck. Can anyone help with this?
Controller:
import angular from 'angular';
const ngRoute = require('angular-route');
import routing from './main.routes';
import serviceModule from '../../components/service/service.module';
export class MainController {
$http;
service;
socket;
awesomeThings = [];
newThing = '';
/*@ngInject*/
constructor($http, $scope, socket, service) {
this.$http = $http;
this.socket = socket;
this.service = service;
$scope.$on('$destroy', function() {
socket.unsyncUpdates('thing');
});
}
$onInit() {
this.stuff = this.service.query();
}
export default angular.module('app.main', [ngRoute, serviceModule])
.config(routing)
.component('main', {
template: require('./main.html'),
controller: MainController
})
.name;
Service:
'use strict';
import angular from 'angular';
import service from './service.service';
export default angular.module('app.services', [])
.factory('service',service)
.name;
Spec:
'use strict';
import main from './main.component';
import serviceModule from '../../components/service/service.module';
import {
MainController
} from './main.component';
describe('Component: MainComponent', function() {
beforeEach(angular.mock.module(main));
beforeEach(angular.mock.module(serviceModule));
beforeEach(angular.mock.module('socketMock'));
var scope;
var mainComponent;
var $httpBackend;
// Initialize the controller and a mock scope
beforeEach(inject(function(_$httpBackend_, $http, $componentController, $rootScope, socket) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('/api/stuff')
.respond([{
title: "My new thing",
description: "This thing is gonna be so awesome dude",
date: "123456",
color: "red"
}]);
scope = $rootScope.$new();
mainComponent = $componentController('main', {
$http: $http,
$scope: scope,
socket: socket
});
}));
it('should attach a list of stuff to the controller', function() {
mainComponent.$onInit();
$httpBackend.flush();
expect(mainComponent.stuff.length)
.to.equal(1);
});
});
Aucun commentaire:
Enregistrer un commentaire