Twitter のクローンである Rails アプリケーションがあります。User モデルと Sessions モデルがあります。ユーザーページの統合テストを作成しており、ファクトリーを使用して (FactoryGirl 経由で) ユーザーを生成しています。
テストでヘルパー メソッドのメソッドを実行できないという問題が発生しています。
次の後にこのエラーが発生しますrspec spec/
。
UserPages FollowUnfollows Post to /relationships click Follow button
Failure/Error: visit user_path(other_user)
ActionView::Template::Error:
undefined method `following? for nil:NilClass
spec/requests/user_pages_spec.rb
:
require 'spec_helper'
describe "UserPages" do
describe "FollowUnfollows" do
describe "Post to /relationships" do
let (:other_user) {FactoryGirl.create(:user)}
let (:user) {FactoryGirl.create(:user)}
before do
sign_in user
end
it "click Follow button" do
visit user_path(other_user)
end
end
end
end
spec/support/utilities.rb
ユーザーのサインインにファイルを使用しています:
include ApplicationHelper
def sign_in(user)
visit signin_path
fill_in "Username", with: user.username
fill_in "Password", with: user.password
click_button "Log In"
cookies[:token] = user.token
end
私は自分のユーザーにアクセスさせる別のユーザー向けの表示ページを持っています -- views/users/show.html.erb
(要約):
<div class="container">
<div class="row-fluid">
<div class="span12">
<div class="span3">
<p><%= @user.username %></p>
<% if @user == current_user %>
<%= render 'tweet_box' %>
<% else %>
<% unless current_user.following?(@user) %>
<%= render 'follow_button' %>
<% else %>
<%= render 'unfollow_button' %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
次に、いくつかの重要なメソッドを含むヘルパーがあります。に含まれてApplicationController
いinclude SessionsHelper
ます。app/helpers/sessions_helper.rb
:
module SessionsHelper
def start_session(user)
cookies.permanent[:token] = user.token
self.current_user=user
end
def user_signed_in?
!current_user.nil?
end
def current_user
@current_user ||= User.find_by_token(cookies[:token])
end
def current_user=(user)
@current_user=user
end
def end_session
self.current_user=nil
cookies.delete(:token)
end
end
Michael Hartl のチュートリアルを実行したことがある人なら誰でも、この多くを認識できます。私は記憶からこれをやろうとしています。また、新しいテストを書き、自分がしていることすべてを理解しようとしています。そういうわけで、私はこれらの質問をしています。ヘルプやフィードバックをいただければ幸いです。