私はあなたがログインするユーザーのメールアドレスとパスワードを使用カピバラで簡単なログインの例のテストをしようとしていて、それはいくつかの理由カピバラについてカピバラ電流パス
「ログイン」予告でroot_urlにリダイレクト私はclick_onログインを使用した後に私がlogin_path "/ login"にいると報告します。しかし、私がそれを実行すると、私はroot_path "/"に私の通知をしています。
カピバラとテストアプリのどちらが欠けていますか? 関連するすべてのコードは以下のとおりです。
コントローラ/ sessions_controller.rb
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "logged in"
else
redirect_to login_path, notice: "Email or password incorrect"
end
end
ビュー/セッション/ new.html.erb
<h1>Sessions Log In</h1>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</div>
<div class="actions"><%= submit_tag "log in" %> </div>
<% end %>
login_spec.rb
it "should let you login with correct password" do
user = Factory.build(:user)
visit login_path
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
click_on "log in"
current_path.should == root_path
page.should have_content("logged in")
end
routes.rbを
...
get 'login', to: 'sessions#new', as: 'login'
...
Factories.rb
Factory.define :user do |f|
f.sequence(:email) {|n| "a#{}@a.a"}
f.password "a"
end
実際にログインしていることを確認できますか?現在のパスをアサートする前に 'page.should have_content("ログイン ")してみてください。 なぜ、作成/保存するのではなく、ユーザーを作成するだけですか。 'Factory.build(:user)'関数が 'Factory(:user)'とは違って実際にはユーザを保存しないことを実感していただきたいと思います。 – prasvin
それはまさに正しいものでした。私はビルドの代わりに作成するつもりだった。 私はそれをチェックしてくださいので、答えとしてそれを入れてください。 – BookOfGreg