【问题标题】:'Confirmation token is invalid' when testing with Cucumber使用 Cucumber 进行测试时出现“确认令牌无效”
【发布时间】:2011-09-25 20:50:01
【问题描述】:

我正在尝试使用 Devise 获取注册表单;我正在用 Cucumber 测试它。

当用户注册时,我会发送一封确认电子邮件。但是,在运行我的测试时出了点问题。

这是我的场景:

  Scenario: Signing in via confirmation
    Given there are the following users:
      | email             | password | unconfirmed |
      | user@ticketee.com | password | true        |
    And "user@ticketee.com" opens the email with subject "Confirmation instructions"
    And they click the first link in the email
    Then I should see "Your account was successfully confirmed."
    And I should see "Signed in as user@ticketee.com"

但是,我收到以下错误:

 expected there to be content "Your account was successfully confirmed. You are now signed in." in "\n      Ticketee\n  \n\nTicketee\nSign up\n    Sign in\nResend confirmation instructions\n\n\n      \n      1 error prohibited this user from being saved:\n      Confirmation token is invalid\n\n\n  Email\n\n  \n\n  Sign inSign upForgot your password?" (RSpec::Expectations::ExpectationNotMetError)

我想这里的重要部分是:

1 error prohibited this user from being saved:\n Confirmation token is invalid

我已经手动对此进行了测试(去注册并单击电子邮件中的确认链接)并且效果很好。只有当我通过 Cucumber 进行测试时,我才收到“确认令牌无效”消息。有谁知道我该如何解决这个问题?我希望看到我的测试通过..

非常感谢。

编辑:在 cmets 中要求的步骤:

When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
  open_email(address, :with_subject => subject)
end

点击链接:

When /^(?:I|they) click the first link in the email$/ do
  click_first_link_in_email
end

我刚刚查看了收到的确认电子邮件,我的电子邮件地址被解析为 mailto: 链接;我将第一个链接步骤更改为:

And they follow "Confirm my account" in the email

但这也不起作用..我仍然收到无效令牌错误消息。

【问题讨论】:

  • 您能否发布涉及打开电子邮件和单击链接的步骤的步骤定义。
  • @AndyWaite 我更新了帖子:)
  • 啊,您使用的是 email_spec gem?可能也值得一提。我相信生成的电子邮件会出现在 test.log 中,因此请在那里检查一下,看看是否有正确的链接。
  • 哦;很抱歉没有提到这一点。我清除了我的 test.log 并运行了测试。看起来它发送了两次电子邮件,但我不太确定。我可以在这里发布日志,但它相当大..
  • 这是日志:pastebin.com/pzTPE1uJ

标签: ruby-on-rails ruby-on-rails-3 devise cucumber actionmailer


【解决方案1】:

所以.. 我在这上面浪费了两个小时。我有一个错字。

我有以下行:

unconfirmed = attributes.delete("unconfirmed") == "true"

我忘了在true 周围加上引号。没有引号,测试通过..

天哪。

感谢所有花时间帮助我的人:)

【讨论】:

  • 你能解释一下那行应该做什么吗?也许有更好的写它不易出错。顺便说一句,这确实应该被单元测试捕获 - 一个很好的例子说明了为什么单独使用 Cucumber 是不够的。
  • 这一行在我的user_steps.rb中;它为用户设置了unconfirmed var,所以我知道哪些用户已经确认了他们的账户,哪些没有。不过,我正在关注一本书,而这行的编写方式对我来说并不是那么清楚,但是你..我将如何为此编写单元测试?我对整个 TDD 有点陌生。我了解如何为控制器等编写 Rspec 测试,但是对于步骤文件?
  • 我遇到了同样的问题,但无论有没有引号都不会通过 true..有什么帮助吗?
  • 我在遵循Rails 3 in action 书中的步骤时犯了同样的错误。感谢您在此处添加问题和答案!
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多