测试未通过:未定义的方法“验证!"对于零:NilClass? [英] Test not passing: undefined method `authenticate!' for nil:NilClass?
问题描述
我有以下失败:
<块引用>失败:1) 使用 Ajax 创建关系的 RelationshipsController 应该增加关系计数失败/错误:xhr:post,:create,关系:{follow_id:other_user.id}无方法错误:未定义的方法认证!"对于零:NilClass# ./spec/controllers/relationships_controller_spec.rb:14:in `block (4 levels) in <top (required)>'# ./spec/controllers/relationships_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
但很奇怪,如果我访问该站点,事情就会起作用(如果我单击 Follow 按钮,followers
计数器会增加:
最奇怪的是没有任何身份验证!relationships_controller_spec.rb 中的方法:
需要'spec_helper'描述 RelationshipsController 做let(:user) { FactoryGirl.create(:user) }let(:other_user) { FactoryGirl.create(:user) }{登录用户}之前描述与 Ajax 建立关系" do它应该增加关系计数"做期待做xhr :post, :create, 关系: {follow_id: other_user.id }end.to change(Relationship, :count).by(1)结尾它应该成功响应"做xhr :post, :create, 关系: {follow_id: other_user.id }response.should be_success结尾结尾描述破坏与 Ajax 的关系" do{ user.follow!(other_user) } 之前让(:关系){ user.relationships.find_by_followed_id(other_user)}它应该减少关系计数"吗期待做xhr :delete, :destroy, id:relationship.idend.to change(Relationship, :count).by(-1)结尾它应该成功响应"做xhr :delete, :destroy, id:relationship.idresponse.should be_success结尾结尾结尾
不在控制器中:
class RelationshipsController <应用控制器before_filter :authenticate_user!定义创建@user = User.find(params[:relationship][:followed_id])current_user.follow!(@user)response_to do |格式|format.html { redirect_to @user }格式.js结尾结尾销毁@user = Relationship.find(params[:id]).followedcurrent_user.unfollow!(@user)response_to do |格式|format.html { redirect_to @user }格式.js结尾结尾结尾
可能是什么问题?
(顺便说一下,这些测试是按照 Ruby on Rails 教程.之后,我删除了所有身份验证系统,因为我想使用设计.)
我必须添加:
spec_helpers.rb:
RSpec.configure 做 |config|config.include Devise::TestHelpers, :type =>:控制器结尾
I have the following failure:
Failures: 1) RelationshipsController creating a relationship with Ajax should increment the Relationship count Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id } NoMethodError: undefined method `authenticate!' for nil:NilClass # ./spec/controllers/relationships_controller_spec.rb:14:in `block (4 levels) in <top (required)>' # ./spec/controllers/relationships_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
But it is very strange, if I visit the site, the thing works (the followers
counter does increment if I click the Follow button:
And the weirdest thing is that there isn't any authenticate! method in relationships_controller_spec.rb:
require 'spec_helper'
describe RelationshipsController do
let(:user) { FactoryGirl.create(:user) }
let(:other_user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "creating a relationship with Ajax" do
it "should increment the Relationship count" do
expect do
xhr :post, :create, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
end
it "should respond with success" do
xhr :post, :create, relationship: { followed_id: other_user.id }
response.should be_success
end
end
describe "destroying a relationship with Ajax" do
before { user.follow!(other_user) }
let(:relationship) { user.relationships.find_by_followed_id(other_user) }
it "should decrement the Relationship count" do
expect do
xhr :delete, :destroy, id: relationship.id
end.to change(Relationship, :count).by(-1)
end
it "should respond with success" do
xhr :delete, :destroy, id: relationship.id
response.should be_success
end
end
end
Neither in the controller:
class RelationshipsController < ApplicationController
before_filter :authenticate_user!
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
What could be the problem?
(By the way, these test were made following the Ruby on Rails Tutorial. After that, I removed all the authentication system because I wanted to use Devise.)
I had to add this:
spec_helpers.rb:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
这篇关于测试未通过:未定义的方法“验证!"对于零:NilClass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!