Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing AngularJS与虚拟路径_Unit Testing_Angularjs - Fatal编程技术网

Unit testing AngularJS与虚拟路径

Unit testing AngularJS与虚拟路径,unit-testing,angularjs,Unit Testing,Angularjs,如何处理AngularJS中的虚拟路径 例如,在我的测试环境中,我的基本url是http://localhost/AppName/在生产中,根目录变成http://www.mysite.com/ 所以在dev中,$http.get会说http://localhost/AppName/Controller/Action但在prod中,该路径不存在,它必须是http://www.mysite.com/Controller/Action 当使用$httpBackend模拟后端时,如何处理$http调用

如何处理AngularJS中的虚拟路径

例如,在我的测试环境中,我的基本url是
http://localhost/AppName/
在生产中,根目录变成
http://www.mysite.com/

所以在dev中,
$http.get
会说
http://localhost/AppName/Controller/Action
但在prod中,该路径不存在,它必须是
http://www.mysite.com/Controller/Action

当使用
$httpBackend
模拟后端时,如何处理
$http
调用以及单元测试

我在
\u Layout.cshtml
(使用ASP.NET MVC)中尝试过这一点,但在单元测试中不起作用:

<script>
    (function () {
        'use strict';

        var app = angular.module('cs').value('urlHelper', {
            getUrl: function (url) {
                var basePath = '@VirtualPathUtility.ToAbsolute("~/")';

                return basePath + url;
            }
        });
    }());
</script>

任何在部署到prod之前不必担心在某处切换根路径的想法?

basePath本身就是一个值,它是一个依赖于basePath的服务

在服务器端生成的页面中,服务器将注入VirtualPath实用性找到的任何值,在测试中,您将手动注入适合您的任何值

剃刀:

<script>
    angular.module('cs').value('basePath', '@VirtualPathUtility.ToAbsolute("~/")');
</script>
测试:


basePath本身可以是一个值,urlHelper可以是一个依赖于basePath的服务

在服务器端生成的页面中,服务器将注入VirtualPath实用性找到的任何值,在测试中,您将手动注入适合您的任何值

剃刀:

<script>
    angular.module('cs').value('basePath', '@VirtualPathUtility.ToAbsolute("~/")');
</script>
测试:


不要为域名操心,从
/
开始。它会自动指向右边的根。@Mik378-你是说用$http发出请求,就像这样:
$http.get('/Controller/Action')
在这两种情况下都有效吗?是的,没错。我在我的应用程序中就是这样工作的。可能会帮助你(类似的问题):(甚至与ASP.NET相比)关于我的第一条评论,你是对的,我误解了这个问题。不要为域名而烦恼,从
/
开始。它会自动指向右边的根。@Mik378-你是说用$http发出请求,就像这样:
$http.get('/Controller/Action')
在这两种情况下都有效吗?是的,没错。我在我的应用程序中就是这样工作的。我可以帮你(类似的问题):(甚至与ASP.NET相比)关于我的第一条评论,你是对的,我误解了这个问题。你能提供一个样本吗?你能提供一个样本吗?
angular.module('cs').factory('urlHelper', function (basePath) {
    return {
        getUrl: function (url) {
            return basePath + url;
        }
    };
});
var urlHelper;

beforeEach(function() {

    module(function($provide) {
        $provide.value('basePath', '/');
    });

    inject(function($injector) {
        urlHelper = $injector.get('urlHelper');
    });
});