LoginSignup
3
3

More than 5 years have passed since last update.

"Railsのユーザ認証deviseを用いた簡単かつ突っ込んだ実装"を試してみる

Last updated at Posted at 2014-02-16

こちらの記事「Railsのユーザ認証deviseを用いた簡単かつ突っ込んだ実装」をRails4で試している。
こちらの記事を書いたema25さんに感謝。
一部コードの編集が必要だったので備忘としてメモ。

修正点

  • factories.rbの変更
  • spec/factories/items.rbの削除
  • spec/controllers/welcome_controller_spec.rbの変更

factories.rbの変更

factory_girlの記載方法が変わっているので下記のように変更。

spec/factories.rb
require 'factory_girl'

FactoryGirl.define do
  factory :user, class: User do
    id 2
    name "Test User"
    email "user@test.com"
    password "foobar"
    password_confirmation "foobar"
  end

  factory :item, class: Item do
    name "Test Item"
    user_id 2
  end
end

spec/factories/items.rbの削除

上記を修正し、rake db:migrate RAILS_ENV=testを実行すると、"Factory already registered: item"というエラーとなる。

rake aborted!
Factory already registered: item

これはテスト用データが重複している、というメッセージ。
itemのデータについては、上記factories.rbで記載しているので、spec/factories/items.rbを削除。

spec/controllers/welcome_controller_spec.rbの変更

こちらもfactory_girlの記載方法が変わっているので下記のように変更。

spec/controllers/welcome_controller_spec.rb
require 'spec_helper'

describe WelcomeController do
  before (:each) do
    user = FactoryGirl.create(:user)
    sign_in user
  end

  describe "GET 'index'" do
    it "should be successful" do
      get 'index'
      response.should be_success
      assigns[:signin].should == "ok"
      # pp response
    end

    it "all items finded" do
      FactoryGirl.create(:item)
      get 'index'
      response.should be_success
      assigns[:items].count.should == 1
      assigns[:items].first.name.should == "Test Item"
    end
  end
end
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3