4

导轨新手。尝试遵循迈克尔哈特尔的教程。

尝试添加辅助方法来模拟 RSpec 测试中的日志时遇到了困难:

describe "when the a user has logged in and attempts to visit the page" do
    let(:user) { FactoryGirl.create :user }
    before do 
      log_in user
    end
    it "should redirect the user to next page" do
      specify { response.should redirect_to loggedin_path }
    end
  end

在我的 spec/support/utilities.rb 中:

def log_in user
    visit root_path
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
    click_button "Log in"
    cookies[:remember_token] = user.remember_token
end

错误:

Failure/Error: log_in user
     NoMethodError:
       undefined method `cookie_jar' for nil:NilClass

是什么赋予了?

编辑,完整的堆栈跟踪:

Index page when the a user has logged in and attempts to visit the page should redirect the user to next page
     Failure/Error: log_in user
     NoMethodError:
       undefined method `cookie_jar' for nil:NilClass
     # ./spec/support/utilities.rb:8:in `log_in'
     # ./spec/features/pages/index_spec.rb:20:in `block (3 levels) in <top (required)>'
4

3 回答 3

3

RSpec 对放置测试的目录非常特别。如果您将测试放在错误的目录中,它不会自动混合设置不同类型测试的各种测试助手。看来您的设置使用spec/features的不是批准的默认目录spec/requestsspec/integrationspec/api)。

根据教程页面,我不确定他们是如何spec_helper.rb设置文件的。尽管这些示例是他们spec/requests用来进行测试的。

您可以强制 RSpec 使用以下选项之一来识别请求规范的另一个目录:

手动将适当的模块添加到测试文件中:

# spec/features/pages/index_spec.rb
require 'spec_helper'

describe "Visiting the index page" do

  include RSpec::Rails::RequestExampleGroup

  # Rest of your test code
  context "when the a user has logged in and attempts to visit the page" do
    let(:user) { FactoryGirl.create :user }

    before do 
      log_in user
    end

    specify { response.should redirect_to loggedin_path }
  end

end

将此包含在您的spec/spec_helper.rb文件中:

RSpec::configure do |c|
  c.include RSpec::Rails::RequestExampleGroup, type: :request, example_group: {
    file_path: c.escaped_path(%w[spec (features)])
  }
end

由于这是一个教程,我建议遵循包含require 'spec_helper'在规范文件顶部的标准,并且您的实际spec/spec_helper.rb文件具有require 'rspec/rails'

一个小提示,您不需要将 aspecify放在it块的内部。它们是彼此的别名,所以只使用一个。

context "when the a user has logged in and attempts to visit the page" do
  let(:user) { FactoryGirl.create :user }

  before do 
    log_in user
  end

  # All of the following are the same

  it "redirects the user to next page" do
    response.should redirect_to loggedin_path
  end

  it { response.should redirect_to loggedin_path }

  specify "redirects the user to next page" do
    response.should redirect_to loggedin_path
  end

  specify { response.should redirect_to loggedin_path }
end

请注意,根据capybara的文档,您应该能够将 capybara 测试放入spec/features. 要完成这项工作,请确保您直接加载require 'capybara/rspec'spec_helper测试规范文件。

但是,查看source,我没有看到它们自动包含该目录的位置。您也可以尝试将标签添加type: :feature到测试文件的外部describe块中。虽然更可能的解决方案是使用spec/requests.

于 2013-05-06T20:27:10.707 回答
0

您不应该在括号中包含方法的“用户”参数吗?

def log_in(user)
    visit root_path
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
    click_button "Log in"
    cookies[:remember_token] = user.remember_token
end
于 2013-05-02T21:46:05.290 回答
-1

要拥有一个模拟饼干罐,rack-testrspec-railsGemfile. 我想也许你已经包括了rspec,也许错过了rspec-rails

您还需要确保已按如下方式配置会话存储:

config.session_store = :cookie_store

这必须config/application.rbconfig/initializers. 如果您在config/environments/development.rb其他地方或其他地方进行了配置,则测试环境将无法获取它。

于 2013-05-05T15:46:08.397 回答