gpt4 book ai didi

javascript - AngularJS - 如何重构我的 Jasmine 规范?

转载 作者:行者123 更新时间:2023-12-03 11:28:02 26 4
gpt4 key购买 nike

我使用 AngularJS 1.2.16 和 Jasmine 2.X 作为我的 javascript 规范。
但他们很快就变得一团糟。我很难找到有关如何重构和构建规范的信息。

以下是我的一些不好的规范:

  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 规范的最佳实践?

最佳答案

使用 BeforeEach 放置每个规范的一些初始公共(public)代码,例如,您可以放置​​这些行:

      deferred = $q.defer()
spyOn(mockRestangular.one().one(), 'get').and.returnValue(deferred.promise)
spyOn(channel::, 'init').and.stub()
new_channel = new channel(channel_id)

BeforeEach 中,与相关的describe 关联。

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
});

关于javascript - AngularJS - 如何重构我的 Jasmine 规范?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26845108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com