【发布时间】:2017-07-19 21:24:05
【问题描述】:
我正在为一个名为 GeneController 的控制器构建一个测试,该控制器使用名为 Gene 的服务来发出一堆 API GET 请求。我已经像这样模拟了主要的 GET 请求,我很确定它工作正常:
$httpBackend.expectGET '/api/knowledge/genes/param1'
.respond(200, JSON.stringify({
data: {
mutationsUrl: 'http://localhost:3000/api/knowledge/genes/param1/mutations',
frequenciesUrl: 'http://localhost:3000/api/knowledge/genes/param1/frequencies',
annotationUrl: 'http://localhost:3000/api/knowledge/genes/param1/annotation',
sections: {
description: {
data: 'holder'
}
}
}
}))
控制器:基因控制器
.controller 'GeneController', Array '$scope', '$routeParams', '$http', 'Gene', ($scope, $routeParams, $http, Gene) ->
$scope.entity = Gene.get($routeParams
(entity) ->
$scope.description = entity.data.sections.description.data
entity.getUrl entity.data.mutationsUrl, {limit: 10}, (err, mutationsData) ->
if ! err?
for m in mutationsData.data
m.selected = true
$scope.mutations = mutationsData
entity.getUrl entity.data.frequenciesUrl, {}, (err, frequenciesData) ->
if ! err?
$scope.frequencies = frequenciesData
entity.getUrl entity.data.annotationUrl, {}, (err, annotationData) ->
if ! err?
$scope.annotation = annotationData
(error) ->
console.log error
)
服务:基因
.factory 'Gene', Array '$resource', '$http', ($resource, $http) ->
Gene = $resource '/api/knowledge/genes/:gene', {},
query:
method: 'GET'
Gene.prototype.getUrl = (url, options, callback) ->
$http {method: 'GET', url: url, params: options}
.then (res) -> # Success callback
callback null, res.data
,(res) -> # Error callback
callback res.status, res.data
Gene
我遇到的问题是由 Gene.prototype.getUrl 方法促进的“辅助”GET 请求。我认为该方法本身运行良好,因为网页上显示了正确的内容(来自突变、频率和注释)。但是,这些 GET 请求失败,我从 mocha 收到以下错误:“未定义不是对象(正在评估 'mutationsData.data')”。
我已经模拟了对这些 GET 请求的响应,但无济于事。这是我对控制器的测试。
测试
...
describe 'GeneController', () ->
Gene = undefined
beforeEach inject (_$controller_, $rootScope, _Gene_, _$httpBackend_) ->
scope = $rootScope.$new()
controller = _$controller_ 'GeneController', {
$scope: scope
$routeParams: { gene: 'param1' }
}
Gene: _Gene_
$httpBackend = _$httpBackend_
describe 'valid response', () ->
beforeEach () ->
$httpBackend.whenGET 'http://localhost:3000/api/knowledge/genes/param1/mutations'
.respond(200, JSON.stringify({
data: "something"
}))
$httpBackend.whenGET 'http://localhost:3000/api/knowledge/genes/param1/frequencies'
.respond(200, JSON.stringify({
data: 'somethingelse'
}))
$httpBackend.whenGET 'http://localhost:3000/api/kowledge/gene/param1/annotation'
.respond(200, JSON.stringify({
data: 'somethingelser'
}))
$httpBackend.expectGET '/api/knowledge/genes/param1'
.respond(200, JSON.stringify({
data: {
mutationsUrl: 'http://localhost:3000/api/knowledge/genes/param1/mutations',
frequenciesUrl: 'http://localhost:3000/api/knowledge/genes/param1/frequencies',
annotationUrl: 'http://localhost:3000/api/knowledge/genes/param1/annotation',
sections: {
description: {
data: 'holder'
}
}
}
}))
$httpBackend.flush()
it "should set $scope.description when the Gene is called", () ->
expect(scope.description).to.equal "holder"
对于这个问题的任何帮助将不胜感激。我完全坚持这一点。提前谢谢你:)
【问题讨论】:
标签: angularjs unit-testing mocha.js chai httpbackend