Javascript AngularJS-如何重构我的茉莉花规格?
我使用AngularJS 1.2.16和Jasmine 2.X作为我的javascript规范。Javascript AngularJS-如何重构我的茉莉花规格?,javascript,angularjs,jasmine,karma-runner,specifications,Javascript,Angularjs,Jasmine,Karma Runner,Specifications,我使用AngularJS 1.2.16和Jasmine 2.X作为我的javascript规范。 但他们很快就变得一团糟。我很难找到关于如何重构和构建规范的信息 以下是我的一些不好的规格: channel = mockRestangular = $httpBackend = deferred = undefined channel_id = {...} beforeEach -> module("channels", ($provide) -> mo
但他们很快就变得一团糟。我很难找到关于如何重构和构建规范的信息 以下是我的一些不好的规格:
channel = mockRestangular = $httpBackend = deferred = undefined
channel_id = {...}
beforeEach ->
module("channels", ($provide) ->
mockRestangular = {
configuration: { baseUrl: "" }
one: ->
this
post: ->
this
put: ->
this
...
}
module ($provide) ->
$provide.value('Restangular', mockRestangular)
return
)
beforeEach inject((_channel_, $q, $injector) ->
channel = _channel_
$httpBackend = $injector.get('$httpBackend')
deferred = $q.defer()
)
it "spec1", inject(($q, $rootScope) ->
deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)
new_channel.updateCount()
deferred.resolve({"channels":[{...long...long...object...}]})
$rootScope.$digest()
expect(new_channel.meta.totalProducts).toEqual(24849)
expect(new_channel.meta.activeProducts).toEqual(1349)
)
it "spec2", inject(($q, $rootScope) ->
deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)
new_channel.updateStatisticsRevenue()
deferred.resolve({"revenue_statistics":[{...another...very...very...long...object...}]})
$rootScope.$digest()
expect(new_channel.statistics.revenue).toEqual([{...kinda...long...object...result...}])
)
# spec with real respond-mock objects
describe "describtor2", ->
it "spec3", inject(($rootScope) ->
$httpBackend.expectPUT().respond(201,
{products:[{"sku":"10413161","active":false,"min_price":{"fractional":400,"currency":"EUR"},"max_price":{"fractional":950,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]})
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel channel_id
new_channel.updateProducts()
new_channel.getMeta().activeProducts = 2
expect(mockRestangular.one().one().get).toHaveBeenCalled
deferred.resolve({"products":[{"sku":"10413161","active":true,"min_price":{"fractional":412,"currency":"EUR"},"max_price":{"fractional":890,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":448,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]}
)
$rootScope.$digest()
new_channel.updateProduct([{sku:"10413161",active:false,min_price:{fractional:400,currency:"EUR"},max_price:{fractional:950,currency:"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}])
$httpBackend.flush()
expect(new_channel.getProducts()).toEqual(
[{"sku":"10413161","active":false,"min_price":{"fractional":400,"currency":"EUR"},"max_price":{"fractional":950,"currency":"EUR"}},{"sku":"10413162","active":true,"min_price":{"fractional":458,"currency":"EUR"},"max_price":{"fractional":799,"currency":"EUR"}}]
)
expect(new_channel.getMeta().activeProducts).toBe(1)
)
因为它们很长,里面有所有的对象,我甚至开始在一个规范中加入更多的“期望值”。我知道这是错误的,但我害怕那些巨大的规范
Jasmine规范的结构化或重构有什么最佳实践吗?在每个规范之前使用
来放置每个规范的一些初始通用代码,例如,您可以放置以下行:
deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)
在每个
之前的中,关联到相关的描述
beforeEach(function() {
deferred = $q.defer();
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise);
spyOn(channel::, 'init').and.stub();
new_channel = new channel(channel_id);
});
其他选择:创建一些基本javascript函数来收集公共代码。
这样做的好处是,您可以命名代码的以下部分:
function mockDBGet() {
deferred = $q.defer();
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise);
}
function initChannel() {
spyOn(channel::, 'init').and.stub();
new_channel = new channel(channel_id);
}
//.......
it('myCurrentSpec', function(){
mockDBGet();
initChannel(); far more clean than your previous version
});
每次之前的都很好。对于每个不知道的人,就像我一样,每个描述都可以在每个函数之前有自己的。另一种选择听起来很有趣,你能给出一些例子/进一步的链接吗?更新为example.Ohh-ja。真漂亮。对于如何处理更大的模拟响应对象,您有什么想法吗?我想如果我们可以加上这一点,你提供了一个a+的答案,关于我和其他可能的问题。这个想法是出现一个等级的规格。看看这个:(Java示例,但与Jasmine非常相似)具有特定设置的“上下文”层次结构支持设置的分解。(beforeach
在茉莉花语中)。=>请毫不犹豫地声明对应于特定上下文的嵌套descripe
,这样您就可以从父级上下文的初始化(mock等)中获益。感谢链接。我只是想知道是否有一个很好的解决方案来存储更大的(200多个字符的模拟响应)。将它们存储在自己的函数上下文中甚至可能还不够。只是分离规格和模拟。有没有最佳实践?