简体   繁体   English

在Ember App Kit中测试ember-simple-auth

[英]Testing ember-simple-auth within Ember App Kit

I'm attempting to perform integration/acceptance tests for my Ember app. 我正在尝试为我的Ember应用程序执行集成/验收测试。 I'm specifically testing user authentication (eg – submitting the login form) and protected pages/states that require authenticated users. 我专门测试用户身份验证(例如,提交登录表单)和需要经过身份验证的用户的受保护页面/状态。

General notes about my app: 有关我的应用的一般说明:

  • Using Ember App Kit 使用Ember App Kit
  • Using ember-simple-auth for authentication 使用ember-simple-auth进行身份验证
  • I have api-stubs for my ember-simple-auth forms to hit using the Devise authorizer. 我有用于我的ember-simple-auth表单的api-stub ,可使用Devise授权器进行匹配。 These work fine when running the app in-browser. 在浏览器中运行应用程序时,这些功能可以正常工作。

I have three problems: 我有三个问题:

1. Devise Authenticator & Ephemeral Storage 1.设计验证器和临时存储

From the ember-simple-auth API, it refers to using Ephemeral storage for tests. 从ember-simple-auth API中,它指的是使用临时存储进行测试。 I have done so, much like this . 我已经这样做了, 就像这样 However, it seems that the sessions is still getting stored in local storage. 但是,会话似乎仍被存储在本地存储中。 If I do not perform localStorage.clear() in each test setup/teardown tests fail because I remain logged in when each test runs after the first. 如果我在每个测试设置/拆卸测试中均未执行localStorage.clear() ,则会失败,因为在第一次测试之后运行每个测试时,我仍保持登录状态。

Am I able to prevent storing the session in local storage between each test when I'm using the Devise authenticator for my app? 当我为我的应用程序使用Devise身份验证器时,是否能够防止在每次测试之间将会话存储在本地存储中?

2. Multiple Acceptance Tests 2.多次验收测试

If I attempt to log in a user in more than 1 test() , my tests spin off into an infinite loop. 如果我尝试使用多个test() 登录用户 ,则我的测试将陷入无限循环。 The first test will pass, but when the second test submits the login form the entire test suite stops and reboots. 第一个测试将通过,但是当第二个测试提交登录表单时,整个测试套件将停止并重新启动。

Integration Test #1 集成测试#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
  )
)

Integration Test #2 集成测试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
  )
)

3. EAK api-stubs & Testem 3. EAK API存根和Testem

EAK's api-stubs do not seem to be available for Testem, so the "log in" process in these acceptance test when run via the command line/Testem fail. EAK的api-stub似乎不适用于Testem,因此通过命令行/ Testem运行时,这些验收测试中的“登录”过程失败。

I attempted setting up sinon.js , but above mentioned issues have prevented me from deciding if it's actually working correctly or not. 我尝试设置sinon.js ,但是上述问题使我无法确定它是否真正正常工作。 What is the best way to successfully stub logging in a user with ember-simple-auth? 用ember-simple-auth成功存根登录的最佳方法是什么? Is it possible to use EAK's api-stubs for Testem? 是否可以将EAK的api-stub用于Testem?

The example for setting the ephemeral store you're referencing above is outdated (if you're using that it basically has no effect and would default to the localStorage store) - for the new API see the API docs here: http://ember-simple-auth.simplabs.com/ember-simple-auth-api-docs.html#Ember-SimpleAuth-setup . 上面设置的临时存储的示例已过时(如果使用的话,它基本上无效,并且默认为localStorage存储)-有关新的API,请参见此处的API文档: http:// ember -simple-auth.simplabs.com/ember-simple-auth-api-docs.html#Ember-SimpleAuth-setup

Maybe fixing that also fixes your 2nd problem (maybe the login form isn't actually displayed in the 2nd test as the user is still logged in anyway as you're using the localStorage store?). 也许该修复程序也可以解决您的第二个问题(也许登录表单实际上并未显示在第二个测试中,因为在您使用localStorage存储库时,无论如何用户仍然登录?)。

With the help of @marcoow and a few other SO questions and GitHub issues I've been able to resolve all my problems: 借助@marcoow以及其他一些SO问题和GitHub问题,我已经能够解决我所有的问题:

1. Devise Authenticator & Ephemeral Storage 1.设计验证器和临时存储

I was using an outdated API option in my code. 我在代码中使用了过时的API选项。 Updating to use the newer API's storeFactory option resolved my session localStorage issue. 更新为使用较新API的storeFactory选项解决了我的会话localStorage问题。

#  app/initializers/simple-auth.coffee
if Ember.testing == true
  options = Ember.merge({ storeFactory: 'session-store:ephemeral' }, options)

2. Multiple Acceptance Tests 2.多次验收测试

This turned out to be related to another library I was loading called fastclick . 原来这与我正在加载的另一个库fastclick有关 After updating my index file to only load this library in non-test environments, my form submission/infinite loop problems went away. 更新索引文件以仅在非测试环境中加载该库后,我的表单提交/无限循环问题就消失了。

// app/index.html
<!-- @if tests=false -->
  <script src="/vendor/emberjs-touch/lib/ember-fastclick.js"></script>
<!-- @endif -->

3. EAK api-stubs & Testem 3. EAK API存根和Testem

I found other people facing similar issues to me on StackOverflow . 我在StackOverflow上发现其他人也面临与我类似的问题 Their questions were eventually responded to/resolved which helped me resolve me own issues. 他们的问题最终得到答复/解决,这有助于我解决自己的问题。 In the comments of this GH issue , there is a link to a work-around to this issue. 此GH问题评论中 ,有指向该问题的变通办法的链接。 Example here . 这里的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM