Capybara访问不起作用,ActionView :: Template :: Error

时间:2015-08-12 09:54:48

标签: ruby-on-rails capybara

我是使用水豚进行测试的新手,在编写我的第一个非常简单的测试时,我收到了一个错误,并且不知道如何修复它

Failure/Error: visit root_path
 ActionView::Template::Error:
   undefined method `[]' for nil:NilClass
     (in ../app/assets/stylesheets/application.css)

这是我的简单测试:

 require 'spec_helper'

 feature "the login process" do

  scenario "signs me in" do
    visit root_path
    expect(page).to have_content "Willkommen!"
  end
end

以下是我路线的有趣部分:

                root GET    /                               root_pages#home
    new_user_session GET    /users/sign_in(.:format)        devise/sessions#new
        user_session POST   /users/sign_in(.:format)        devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)       devise/sessions#destroy
       user_password POST   /users/password(.:format)       devise/passwords#create
   new_user_password GET    /users/password/new(.:format)   devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)  devise/passwords#edit
                     PATCH  /users/password(.:format)       devise/passwords#update
                     PUT    /users/password(.:format)       devise/passwords#update

我的spec_helper.rb

require 'rubygems' 
ENV["Rails_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'factory_girl_rails'
require 'database_cleaner'

require 'capybara/rspec'
require 'capybara/rails'

RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
DatabaseCleaner.strategy = :truncation
config.before(:suite) do 
  begin 
    DatabaseCleaner.start
    FactoryGirl.lint
  ensure
    DatabaseCleaner.clean
  end
end
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
  # This option will default to `true` in RSpec 4. It makes the `description`
  # and `failure_message` of custom matchers include text for helper methods
  # defined using `chain`, e.g.:
  #     be_bigger_than(2).and_smaller_than(4).description
  #     # => "be bigger than 2 and smaller than 4"
  # ...rather than:
  #     # => "be bigger than 2"
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
 config.mock_with :rspec do |mocks|
  # Prevents you from mocking or stubbing a method that does not exist on
  # a real object. This is generally recommended, and will default to
  # `true` in RSpec 4.
  mocks.verify_partial_doubles = true
  end

我希望这就是你需要的一切。如果缺少某些内容,请在下方发表评论。谢谢你的帮助。

所以这是我的root_page_controller,它或多或少是空的。

class RootPagesController < ApplicationController
  def home

  end
end

和application_controller,也许这很有用:

class ApplicationController < ActionController::Base
 # Prevent CSRF attacks by raising an exception.
 # For APIs, you may want to use :null_session instead.
 protect_from_forgery with: :exception
 helper_method :current_order

 before_action :configure_permitted_parameters, if: :devise_controller?

 def current_order
  if !session[:order_id].nil?
    Order.find(session[:order_id])
  else
    Order.new
  end
 end

protected
def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :postal,     :city, :street, :password, :password_confirmation, :remember_me) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :email, :postal,     :city, :street, :password, :remember_me) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :postal, :city, :street, :password, :password_confirmation, :current_password) }
   end
 end

这是root_pages / home.html.erb

  <h1>Willkommen bei Foodle!</h1>

和application.html

<!DOCTYPE html>
<html>
<head>
  <title>Foodle</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %> 
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
 </head>
<body>
  <%= render 'layouts/header' %>

  <p class="notice"><%= notice %></p>
  <p class="alert"><%= alert %></p>
  <div class="container">
    <div class="text-right">
      <h1 class="cart-text"><%= render 'layouts/cart_text' %></h1>
    </div>
   <%= yield %>
  </div>

</body>
</html>

和_header.html.erb

<%= nav_bar fixed: :top, responsive: true do %>
  <%= menu_group do %>
    <%= menu_item "Foodle", root_path %>
    <%= menu_item "Bestellen", articles_path %>
    <%= menu_item "Alle Nutzer", users_path %>
  <% end %>

  <%= menu_group :pull => :right do %>
  <% if current_user.try(:admin?) %>
    <%= menu_item "(Admin)" %>
  <% end %>
      <% if current_user %>
        <%= drop_down "Account" do %>
          <%= menu_item "Profil bearbeiten", edit_user_path(current_user) %>
          <%= menu_item "Passwort ändern", edit_user_registration_path(current_user) %>
          <%= menu_item "Abmelden", destroy_user_session_path, method: :delete %>
        <% end %>
      <% else %>
          <%= menu_item "Anmelden", new_user_session_path %>
          <%= menu_item "Registrieren", new_user_registration_path %>
      <% end %>
  <% end %>
<% end %>

1 个答案:

答案 0 :(得分:2)

因此,实际的解决方法是将application.css重命名为application.scss