Saturday, April 14, 2012

devise認証やってみる(2/12)MVCの流れとテスト [rails3]

CommunityGuides 2/12 - Model-View-Controller Principle, Routes and Tests http://www.communityguides.eu/articles/2


MVCの流れ
(省略、、、。本家に図があります)

例えば、controllerのshowメソッドではこんなふうに@articleを定義

def show
  @article = Article.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml =>@article }
  end 
end


例えば、route.rbではメソッドをルーティング(?) config/routes.rb
Communityguides::Application.routes.draw do
  devise_for :users

  resources :articles

  root :to =>"articles#index" 
end

% rake routes でroute一覧を表示できる。

articles GET    /articles(.:format)               articles#index
#(/articlesへのGETリクエストはindexメソッドで処理される。(articles_path) )
            POST   /articles(.:format)               articles#create
#(同じパスへのPOSTリクエストはcreateメソッドにより処理。)

new_article GET    /articles/new(.:format)           articles#new
#(/articles/new へのGETリクエストはnewメソッドで処理される(new_article_path) )

など。。。



テスト(初めての)

自動生成テストの修正
deviseの認証が必要なメソッドがある
devise helpersをtest_helpers.rbに追加

test_helper.rb

class ActionController::TestCase
  include Devise::TestHelpers
end


config/environments/test.rb に
config.action_mailer.default_url_options = { :host => 'localhost:3000' }



test/fixtures/users.yml

user1:
  id: 1
  email: user1@communityguides.eu
  encrypted_password: abcdef1
  #password_salt:  efvfvffdv
  confirmed_at: <%= Time.now %>

user2:
  id: 2
  email: user2@communityguides.eu
  encrypted_password: abcdef2
  #password_salt:  hjujzjjzt
  confirmed_at: <%= Time.now %>

user3:
  id: 3
  email: user3@communityguides.eu
  encrypted_password: abcdef3
  #password_salt:  gheureuf
  confirmed_at: <%= Time.now %>

testに使うダミーusers?

test/functional/articles_controller_test.rb
書いてある通りに。。。

require 'test_helper'

class ArticlesControllerTest < ActionController::TestCase
  setup do
    @article = articles(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:articles)
  end

  test "should get new" do
    sign_in users(:user2)
    get :new
    assert_response :success
  end

  test "should create article" do
    sign_in users(:user2)
    assert_diffrence('Article.count') do
      post :create, :article => @article.attributes
    end

    assert_redirected_to article_path(assings(:article))
  end

  test "should show article" do
    get :show, :id => @article.to_param
    assert_response :success
  end

  test "should get edit" do
    sign_in users(:user2)
    get :edit, :id => @article.to_param
    assert_response :success
  end

  test "should update article" do
    sign_in users(:user2)
    put :update, :id => @article.to_param, :article => @article.attributes
    assert_redirected_to article_path(assigns(:article))
  end

  test "should destroy article" do
    sign_in users(:user2)
    assert_difference('Article.count', -1) do
      delete :destroy, :id => @article.to_param
    end

    assert_redirected_to articles_path
  end
end


% rake db:test:prepare
% rake

=> 7 tests, 0 assertions, 0 failures, 7 errors, 0 skips

ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password_salt:
と出るのでusers.ymlからpassword_saltの部分をコメントアウト。

schema.rbでuserテーブル確認してもなかったので、deviseのバージョンによる違いかなと。。。

7 tests, 10 assertions, 0 failures, 0 errors, 0 skips

テストか。。。

No comments:

Post a Comment