简体   繁体   English

Rails url helper在Rspec中不起作用

[英]Rails url helpers not working in Rspec

I am new to programming, this is my first application. 我是编程新手,这是我的第一个应用程序。 While creating an application in Rails, i have two models. 在Rails中创建应用程序时,我有两个模型。 User&list,nested. 用户列表中,嵌套。

resources :users do
  resources :lists
end

These are the following routes i obtain with this setting: 这些是我使用此设置获得的以下路线:

user_lists GET    /users/:user_id/lists(.:format)             lists#index
           POST   /users/:user_id/lists(.:format)             lists#create
new_user_list GET    /users/:user_id/lists/new(.:format)      lists#new
edit_user_list GET    /users/:user_id/lists/:id/edit(.:format)lists#edit
user_list GET    /users/:user_id/lists/:id(.:format)          lists#show
           PUT    /users/:user_id/lists/:id(.:format)         lists#update
           DELETE /users/:user_id/lists/:id(.:format)         lists#destroy

With regards i have created the views with the following links. 关于我已经用以下链接创建了视图。

<div class="stats">
  <a href="<%= user_lists_path(current_user) %>">
    <%= pluralize(current_user.lists.count, 'List') %>
  </a>
</div>

<div class="list">
  <%= link_to 'Create List', new_user_list_path(current_user) %>
</div>

These work as expected, however when i use the same url helpers in testing i get an error. 这些工作按预期工作,但是当我在测试中使用相同的url助手时,我得到一个错误。

describe "List create page" do 
  let(:user) { FactoryGirl.create(:user) }
  before do  
    user.save
    visit new_user_list_path(user)
  end
  it { should have_selector('title', text: 'SocialTask | List') }
  it { should have_selector('h1', text: 'Create list') }

  describe "invalid list creation" do 
    before { click_button 'Create list' } 
    it { should have_content('Error in creating list') }
  end
 end

This causes the tests to have an error. 这会导致测试出错。

Failure/Error: visit new_user_list_path(user)
 NoMethodError:
   undefined method `lists' for nil:NilClass

I have tried playing around with the url that did not work. 我试过玩弄无效的网址。 I tried updating rspec/capybara that did not work either. 我尝试更新无法正常工作的rspec / capybara。 I have also checked the inclusion of 我也检查了包含

config.include Rails.application.routes.url_helpers

in the spec helper. 在spec帮助器中。

How do i get the helpers to work? 我如何让助手工作? Or am i missing some minor detail? 或者我错过了一些细节? Thanks in advance. 提前致谢。

Helper Methods. 助手方法。

module SessionsHelper

def sign_in(user)
  cookies.permanent[:remember_token] = user.remember_token
  self.current_user = user 
end

def current_user=(user)
  @current_user = user 
end

def current_user 
  @current_user ||= User.find_by_remember_token(cookies[:remember_token])
end

def signed_in?
  !current_user.nil?
end

def sign_out
  self.current_user = nil 
  cookies.delete(:remember_token)
end

def current_user?(user)
  current_user == user
end
end

The rspec helper to sign in. support/utilities.rb 登录的rspec助手.supports / utilities.rb

include ApplicationHelper
 def sign_in(user)
  visit signin_path
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  cookies[:remember_token] = user.remember_token
 end

Without seeing the stack trace, I think your problem is in the view on this line: 没有看到堆栈跟踪,我认为你的问题在这一行的视图中:

<%= pluralize(current_user.lists.count, 'List') %>

It seems like current_user is nil. 看起来current_user是零。 Normally you should define some kind of helper method in your RSpec suite to simulate a user logging in. That way, current_user will return the user that you stub out in the test. 通常,您应该在RSpec套件中定义某种辅助方法来模拟用户登录。这样, current_user将返回您在测试中存根的用户。

Here's an example: 这是一个例子:

# spec/support/session_helper.rb
module SessionHelper
  def login(username = 'admin')
    request.session[:user_id] = User.find_by_username(username).id
  end
end

Yours will differ depending on how you authenticate your users. 根据您对用户进行身份验证的方式,您的用户会有所不同。 For example, Devise publishes its own set of test helpers, so you can simply include its helpers directly: 例如,Devise发布了自己的一组测试助手,因此您可以直接包含其助手:

# spec/support/devise.rb
RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

Seems it's getting an error because the user doesn't exists. 似乎它因为用户不存在而收到错误。 Try to change user.save to user.save! 尝试将user.save更改为user.save! then you'll catch the error on creation I think.. 然后你会发现我认为创造的错误..

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

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