次の失敗があります。
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)>'
しかし、非常に奇妙です。サイトにアクセスすると、機能します ( [フォローfollowers
] ボタンをクリックすると、カウンターが増加します:
そして、最も奇妙なことは、認証がないことです! relationship_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
コントローラーでも:
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
何が問題なのですか?
(ちなみに、これらのテストはRuby on Rails チュートリアルに従って行われました。その後、Deviseを使用したかったので、すべての認証システムを削除しました。)