admin管理员组文章数量:1386709
So i have done quite a research already before asking this and none of them is what i want.
I have an app bootstrapped with angular module. The controller inside the module makes some http requests. Now i am unit testing my app for ui and i need to mock the http requests. All the mocking that i have read is done with jasmine, but i don't want to use it.
I want that whenever i open the app, all the requests get mocked. I have already tried angular mock and the backend mocks, none of them workes. And i dont want to prepopulate the scope elements because the code should be deployment ready.
So i have done quite a research already before asking this and none of them is what i want.
I have an app bootstrapped with angular module. The controller inside the module makes some http requests. Now i am unit testing my app for ui and i need to mock the http requests. All the mocking that i have read is done with jasmine, but i don't want to use it.
I want that whenever i open the app, all the requests get mocked. I have already tried angular mock and the backend mocks, none of them workes. And i dont want to prepopulate the scope elements because the code should be deployment ready.
Share Improve this question asked Sep 18, 2013 at 16:31 Shivam ShahShivam Shah 5245 silver badges10 bronze badges 5- You need to mock http requests, or server responses to those requests? – jusio Commented Sep 18, 2013 at 17:58
- You could always set up a service that makes the http calls instead of making them directly from the controller. If you call the service from the controller then you can easily mock the service. – bmceldowney Commented Sep 18, 2013 at 18:53
- I need to mock the response – Shivam Shah Commented Sep 18, 2013 at 19:03
- Yes you do. If the response is provided by an angular service that you define instead of $http then it will be easier for you to mock the response. – bmceldowney Commented Sep 18, 2013 at 19:28
- Need more information. You say that angular mocks doesn't work, but it does. I've used it on plenty of projects, so it sounds more like either you don't now how to use it or have an issue. What is it exactly you are doing that isn't working? If you inject the $httpBackend service and configure it, you can mock the requests as needed. – Jeremy Likness Commented Sep 18, 2013 at 19:58
1 Answer
Reset to default 7If you want to mock your backend during development, just install angular-mocks
in your main html file, add it up as a dependency in your application (angular.module('myApp', ['ngMockE2E'])
) and then mock the requests you need to.
E.g.
angular.module('myApp')
.controller('MainCtrl', function ($scope, $httpBackend, $http) {
$httpBackend.whenGET('test').respond(200, {message: "Hello world"});
$http.get('test').then(function(response){
$scope.message = response.message //Hello world
})
});
Be wary though, that adding the ngMockE2E
will require you to set up your routes in case you do so through AngularJS routing.
E.g.
angular.module('myApp', ['ngMockE2E'])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
})
.run(function($httpBackend){
$httpBackend.whenGET('views/main.html').passThrough();
})
本文标签: javascriptMocking angularjs http requestsStack Overflow
版权声明:本文标题:javascript - Mocking angularjs http requests - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744558080a2612599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论