[英]Equivalent of Ember Fixture Adapter in AngularJS
Has anyone implemented something like the ember fixture adapter in AngularJS for mocking the backend during development? 是否有人在AngularJS中实现了诸如ember Fixture适配器之类的东西来在开发过程中模拟后端?
I see that AngularJS has the $httpBackend for doing unit tests, but can this be used like the ember fixture adapter to run the actual application? 我看到AngularJS具有进行单元测试的$ httpBackend ,但是可以像ember Fixture适配器一样使用它来运行实际的应用程序吗?
If there is nothing currently available, how could this be implemented in a way that allows for an easy transition between fixtures and an actual backend (for dev and prod environments)? 如果当前没有可用的设备,那么如何以一种允许在固定装置和实际后端之间轻松转换(对于开发和生产环境)的方式实现呢?
Personally I prefer using mockjax( https://github.com/appendto/jquery-mockjax ). 我个人更喜欢使用嘲笑( https://github.com/appendto/jquery-mockjax )。
$.mockjax({
url: '/colors',
dataType: 'json',
responseText: {
colors:[
{
id: 1,
color: "red"
},
{
id: 2,
color: "green"
},
{
id: 3,
color: "blue"
}
]
}
});
Here's an ember example, but it'd work the exact same in angular. 这是一个余烬示例,但是它在角度上的作用完全相同。
http://emberjs.jsbin.com/OxIDiVU/73/edit http://emberjs.jsbin.com/OxIDiVU/73/edit
It turns out that AngularJS has two services called $httpBackend; 事实证明,AngularJS有两个名为$ httpBackend的服务。 one for unit testing, and the other one is meant for e2e testing or "backend-less" development.
一个用于单元测试, 另一个用于e2e测试或“无后端”开发。 So you can do the following (adapted from the AngularJS docs )
因此,您可以执行以下操作(改编自AngularJS docs )
var myApp = angular.module('myApp', []);
//...
var myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
myAppDev.run(function($httpBackend) {
phones = [{name: 'phone1'}, {name: 'phone2'}];
// returns the current list of phones
$httpBackend.whenGET('/phones').respond(phones);
// adds a new phone to the phones array
$httpBackend.whenPOST('/phones').respond(function(method, url, data) {
var phone = angular.fromJson(data);
phones.push(phone);
return [200, phone, {}];
});
$httpBackend.whenGET(/^\/templates\//).passThrough();
//...
});
angular.bootstrap ($('html'), ['myApp', 'myAppDev']);
Make sure you include a refernce to the ng-mock library. 确保包括对ng-mock库的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.