1
新しいコンテンツを作成するユーザーのための機能テストに問題があります。私のコントローラでこの行をコメントしたり削除したりするときにだけ渡されます。ファクトリのように見えます。ユーザーは新しいコンテンツを作成する能力を持っていません。私は、新しい単語を作成する能力を持っているかどうかを確認するために、別のテストを使って工場をチェックしました。何が問題なの?Rspecを使用しているときのCancancan gem
authorize! :create, @word, :message => "Musisz się zalogować, aby dodać nowe słówko."
words_controller.rb
def create
authorize! :create, @word, :message => "Musisz się zalogować, aby dodać nowe słówko."
@user = current_user
@word = @user.words.build(word_params)
respond_to do |format|
if @word.save
format.html {
redirect_to new_word_path
flash[:notice] = 'Słówko zostało prawidłowo zapisane. Dodaj następne!'
}
format.json { render :show, status: :created, location: @word }
else
format.html { render :new }
format.json { render json: @word.errors, status: :unprocessable_entity }
end
end
end
user_creates_word_spec.rb
require 'spec_helper'
feature 'User creates word' do
before(:all) do
category = FactoryGirl.create(:category)
10.times { word = FactoryGirl.create(:word) }
sign_in
expect(page).to have_content 'Wyloguj się'
visit game_words_path
click_link('Dodaj słówko')
end
scenario 'with valid input' do
fill_in 'Angielski', with: 'house'
fill_in 'Polski', with: 'dom'
find(:css, ".check_boxes[value='1']").set(true)
click_button 'Zapisz'
expect(page).to have_content 'Słówko zostało prawidłowo zapisane.'
end
end
テストユーザーの工場は、私はこの行を変更し
it "can create new word" do
user = FactoryGirl.create(:user)
ability = Ability.new(user)
word = Word.new(user: user)
expect { ability.should be_able_to(:create, word)}
end