【问题标题】:protractor different mock httpbackend responses量角器不同的模拟 httpbackend 响应
【发布时间】:2016-01-27 10:29:58
【问题描述】:

我正在使用模拟模块进行一些测试。

当页面被加载时,例如

    browser.get('http://localhost:5643/#/balance/import');

下面的这个 api url 被调用,我们得到下面的响应,它工作得很好。

$httpBackend.whenGET('https://localhost:44329/api/daystatus').respond(
                {
                    'DayID': 249,
                    'weekend': false,
                    'dayStatusTypeID': 5,
                    'balance': null
                }
            );

但是当另一个页面被加载时,例如

    browser.get('http://localhost:5643/#/dashboard');

并且还调用了mock模块中的api url

但这次我希望它返回不同的响应。 (因为上一页已加载,并且发生了一些 UI 测试操作。)

$httpBackend.whenGET('https://localhost:44329/api/daystatus').respond(
                {
                    'DayID': 249,
                    'weekend': false,
                    'dayStatusTypeID': 7,
                    'balance': null
                }
            );

我如何在我的模拟模块中说这次不是第一次使用对 API url 的第二次调用?目前我加载它使用的任何页面:

$httpBackend.whenGET('https://localhost:44329/api/daystatus').respond(
                {
                    'DayID': 249,
                    'weekend': false,
                    'dayStatusTypeID': 5,
                    'balance': null
                }
            );

我可以检测请求来自哪个页面..或者我可以传入查询字符串变量吗?不知道该怎么做。

【问题讨论】:

    标签: angularjs unit-testing protractor gulp-protractor


    【解决方案1】:

    您可以通过Referer标头区分API调用:

    $httpBackend.whenGET('https://localhost:44329/api/daystatus', 
                         {'Referer': 'http://localhost:5643/#/balance/import'})
    

    和:

    $httpBackend.whenGET('https://localhost:44329/api/daystatus', 
                         {'Referer': 'http://localhost:5643/#/dashboard'})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多