rspecとcapybaraが新しく、テストシナリオを徐々に拡張して学習します。Rspec:ログインしたユーザーとは異なるユーザーが作成したコメント
ログインした後に作成したコメント(例:Friend)が、別のテストユーザー(例:Test1)によって作成されたように表示される理由を理解できませんか?
(私はブラウザでチェックして、現在のユーザーが作成したとしてコメントが示されている)
require 'rails_helper'
describe 'navigate' do
let!(:user) { FactoryGirl.create(:user) }
let!(:friend) { FactoryGirl.create(:friend) }
let!(:article) {
Article.create(title:"To test notifications", summary: "Summary of article", description: "Description of article.", status: "published", user_id: user.id)
}
describe 'notification created if other use comments on article' , js: true do
before do
login_as(friend, :scope => :user)
visit article_path(article)
fill_in 'comment[content]', with: "Some comments"
click_on "Add Comment"
logout(:friend)
login_as(user, :scope => :user)
end
it "renders notification on dropdown list" do
expect(page).to have_css("#notificationList", text: friend.name + " posted a Comment on " + article.title)
expect(page).to have_css("#notification-counter", text: "1")
end
it "render notification on notifiction index page" do
visit notifications_path
expect(page).to have_css("#notifications-container", text: friend.name + " posted a Comment on " + article.title)
end
end
end
私も法の下に試してみましたが、私はアプリにログインすることができませんので、テストは失敗します。
2)login_as(friend, :scope => :user)
login_as(friend, :scope => :friend)
とユーザーの工場に置き換え:あなたが原因となっているあなたのコントローラ/ビューで論理エラーを持っていないと仮定すると、
FactoryGirl.define do
sequence :email do |n|
"test#{n}@example.com"
end
factory :user do
sequence(:name) { |n| "Test#{n}" }
email { generate :email }
password "asdfasdf"
password_confirmation "asdfasdf"
end
factory :friend, class: "User" do
name 'Friend'
email { generate :email }
password "asdfasdf"
password_confirmation "asdfasdf"
end
end
こんにちはとして、それを書いたほうが良いと思います、THAアドバイスのためのnks!私は、友人のコメントがログアウトする前に正常に提出されるように、コメント(コメント)をclick_onの後にexpect(page).to have_content "Some comments"を配置しました。しかし、私は今、ログアウト(:friend)をexpect文の後に置いても、Friendをログアウトすることはできません。テストは、ログアウトして手動でログインした場合(つまり、ログアウトボタンをクリックしてサインインフォームに記入) – doyz
記事作成者に通知のライブアップデートを許可するようにアクション設定可能ですが、この問題にリンクされているのでしょうか? – doyz
@diana 'logout'が' devise'/'warden'からのものであれば、その範囲をパラメータ(ユーザではない)として取るので、あなたの場合は' logout(:user) 'でなければなりません - :friend:あなたが持っていないので、:友人のスコープ、ちょうど '友達のユーザーインスタンス –