![](/img/trans.png)
[英]Testing Ember Simple Auth in Ember App Kit with sinon
[英]Testing ember-simple-auth within Ember App Kit
我正在嘗試為我的Ember應用程序執行集成/驗收測試。 我專門測試用戶身份驗證(例如,提交登錄表單)和需要經過身份驗證的用戶的受保護頁面/狀態。
有關我的應用的一般說明:
我有三個問題:
從ember-simple-auth API中,它指的是使用臨時存儲進行測試。 我已經這樣做了, 就像這樣 。 但是,會話似乎仍被存儲在本地存儲中。 如果我在每個測試設置/拆卸測試中均未執行localStorage.clear()
,則會失敗,因為在第一次測試之后運行每個測試時,我仍保持登錄狀態。
當我為我的應用程序使用Devise身份驗證器時,是否能夠防止在每次測試之間將會話存儲在本地存儲中?
如果我嘗試使用多個test()
登錄用戶 ,則我的測試將陷入無限循環。 第一個測試將通過,但是當第二個測試提交登錄表單時,整個測試套件將停止並重新啟動。
集成測試#1
App = null
module('Acceptance - Page #1',
setup: ->
App = startApp()
teardown: ->
Ember.run(App, 'destroy')
)
test('page #1 behind authentication', ->
expect(1)
visit('/page-1')
fillIn('input#identification', 'foo@bar.com')
fillIn('input#password', 'password')
click('button[type="submit"]')
andThen(->
equal(true, true) # This test works fine
)
)
集成測試2
App = null
module('Acceptance - Page #2',
setup: ->
App = startApp()
teardown: ->
Ember.run(App, 'destroy')
)
test('page #2 behind authentication', ->
expect(1)
visit('/page-2')
fillIn('input#identification', 'foo@bar.com')
fillIn('input#password', 'password')
click('button[type="submit"]')
andThen(->
equal(true, true) # Never runs, tests start over, infinite loop begins
)
)
EAK的api-stub似乎不適用於Testem,因此通過命令行/ Testem運行時,這些驗收測試中的“登錄”過程失敗。
我嘗試設置sinon.js ,但是上述問題使我無法確定它是否真正正常工作。 用ember-simple-auth成功存根登錄的最佳方法是什么? 是否可以將EAK的api-stub用於Testem?
上面設置的臨時存儲的示例已過時(如果使用的話,它基本上無效,並且默認為localStorage存儲)-有關新的API,請參見此處的API文檔: http:// ember -simple-auth.simplabs.com/ember-simple-auth-api-docs.html#Ember-SimpleAuth-setup 。
也許該修復程序也可以解決您的第二個問題(也許登錄表單實際上並未顯示在第二個測試中,因為在您使用localStorage存儲庫時,無論如何用戶仍然登錄?)。
借助@marcoow以及其他一些SO問題和GitHub問題,我已經能夠解決我所有的問題:
我在代碼中使用了過時的API選項。 更新為使用較新API的storeFactory
選項解決了我的會話localStorage
問題。
# app/initializers/simple-auth.coffee
if Ember.testing == true
options = Ember.merge({ storeFactory: 'session-store:ephemeral' }, options)
原來這與我正在加載的另一個庫fastclick有關 。 更新索引文件以僅在非測試環境中加載該庫后,我的表單提交/無限循環問題就消失了。
// app/index.html
<!-- @if tests=false -->
<script src="/vendor/emberjs-touch/lib/ember-fastclick.js"></script>
<!-- @endif -->
我在StackOverflow上發現其他人也面臨與我類似的問題 。 他們的問題最終得到答復/解決,這有助於我解決自己的問題。 在此GH問題的評論中 ,有指向該問題的變通辦法的鏈接。 這里的例子 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.