在Rails 3.2.3中使用Cucumber和RSpec测试http基本身份验证

在Rails 3.2.3中使用Cucumber和RSpec测试http基本身份验证,cucumber,capybara,bdd,ruby-on-rails-3.2,rspec-rails,Cucumber,Capybara,Bdd,Ruby On Rails 3.2,Rspec Rails,我想在Rails 3.2.3中测试内置的基本http身份验证机制。我尝试在RSpec和Cucumber中测试http身份验证,但在这两个工具中都失败了。在Cucumber中,我在运行我的功能时收到以下消息: When I perform HTTP authentication as "admin" with "test" # features/step_definitions/web_steps.rb:1 undefined method `env' for nil:NilClass (No

我想在Rails 3.2.3中测试内置的基本http身份验证机制。我尝试在RSpec和Cucumber中测试http身份验证,但在这两个工具中都失败了。在Cucumber中,我在运行我的功能时收到以下消息:

When I perform HTTP authentication as "admin" with "test" # features/step_definitions/web_steps.rb:1
  undefined method `env' for nil:NilClass (NoMethodError)
  ./features/step_definitions/web_steps.rb:3:in `/^I perform HTTP authentication as "([^\"]*)" with "([^\"]*)"$/'
  features/sign_in_http.feature:4:in `When I perform HTTP authentication as "admin" with "test"'
这是我的ApplicationController类:

class ApplicationController“admin”,:password=>“test”
结束
Cucumber中的步骤定义:

When /^I perform HTTP authentication as "([^\"]*)" with "([^\"]*)"$/ do |username, password|
  @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
  visit '/'
end
黄瓜的特点是:

Feature: Signing in via http

Scenario: HTTP sign-in
  When I perform HTTP authentication as "admin" with "test"
  Then I should see "Welcome to the app."
在RSpec中,http身份验证步骤似乎已通过,但我从后续步骤中得到消息,即由于“访问被拒绝”,无法在页面上找到预期内容:

我还尝试在cucumber步骤中访问http授权行之前的根路径,但这给了我关于@request的NilClass的相同消息。 使用定义的“admin:test”凭据通过浏览器登录没有问题,我可以看到我的应用程序的根页面。


如果有任何建议,我将不胜感激。

以下是我是如何让它发挥作用的。关键是在访问任何页面之前运行身份验证步骤,否则您的身份验证已经失败

在我的专题中,我提出:

Background:
    Given I perform HTTP authentication as "<id>/<password>"
    When I go to the homepage
    Then I should see "Text-that-you-should-see-on-your-home-page"

这是我如何让它工作的。关键是在访问任何页面之前运行身份验证步骤,否则您的身份验证已经失败

在我的专题中,我提出:

Background:
    Given I perform HTTP authentication as "<id>/<password>"
    When I go to the homepage
    Then I should see "Text-that-you-should-see-on-your-home-page"

现在有一个更简单的解决方案。我不知道它是什么时候实现的,但是Cucumber现在有一个方法
basic\u authorize
来处理这个问题。以下是我使用的步骤:

give/^I使用用户名“([^”]*)”和密码“([^”]*)”$/do |用户名、密码执行基本HTTP身份验证|
基本授权(用户名、密码)
结束
下面是一个示例用例:

Given I perform basic HTTP authentication with username "cool_user" and password "hunter22"
与JESii的回答一样,在访问任何页面之前,您需要执行此步骤


我希望这能为其他人节省一些时间,我花了很长时间在这上面。

现在有一个更简单的解决方案。我不知道它是什么时候实现的,但是Cucumber现在有一个方法
basic\u authorize
来处理这个问题。以下是我使用的步骤:

give/^I使用用户名“([^”]*)”和密码“([^”]*)”$/do |用户名、密码执行基本HTTP身份验证|
基本授权(用户名、密码)
结束
下面是一个示例用例:

Given I perform basic HTTP authentication with username "cool_user" and password "hunter22"
与JESii的回答一样,在访问任何页面之前,您需要执行此步骤

我希望这能为其他人节省一些时间,我花了很多时间在这上面