テスト駆動-02ユニットテスト

$ruby script/generate migration expand_text

/002_expand_text.rb

class ExpandText < ActiveRecord::Migration
  def self.up
    change_column :members, :text, :string, :limit => 8
  end

  def self.down
    change_column :members, :text, :string, :limit => 16
  end
end

/test/unit/member_test.rb

require File.dirname(__FILE__) + '/../test_helper'

class MemberTest < Test::Unit::TestCase
  fixtures :members

  # Replace this with your real tests.
  def test_truth
    assert true
  end
  def test_text_length
    members(:one).text = '+' * 256
    assert members(:one).valid?, 'for 16 chars'
    members(:one).text = '+' * 257
    assert members(:one).valid?, 'for 17 chars'
  end
end

エラーがなければ、下記のような形になります。

$ rake test:units
(in /Users/mono/dev/rails/chap0425)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I"/Users/mono/dev/rails/chap0425/lib" -I"/Users/mono/dev/rails/chap0425/test" "/Library/Ruby/Gems/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader.rb" "test/unit/member_test.rb" 
Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.4/lib/rake/rake_test_loader
Started
..
Finished in 0.054064 seconds.

2 tests, 3 assertions, 0 failures, 0 errors
mono-no-macbook:chap0425 mono$ 

テスト駆動-01

テストデータベースの作成。

$ rake RAILS_ENV=test setup_db

chap_testが作成済み
/test/functional/main_controller_test.rb

    @first_id = members(:one).id
#:first から :one へ変更する。
    post :create, :member => {:text => 'テストです。',:create => '2009-04-24 12:01:34'}

/test/fixtures/members.yml

one:
  id: 1
  text: mono
  create_at: '2009-04-24 12:01:35'
two:
  id: 2
  text: second
  create_at: '2009-04-24 12:01:36'
$rake
1 tests, 1 assertions, 0 failures, 0 errors

scaffold-03再度まとめ

aptanaでNewprojectとして作成する。
mysqlへログインする。

$mysql5 -u root
mysql> show databases;

/lib/tasks/base.rakeを下記の内容にて作成する。

env = ENV['RAILS_ENV'] || 'development'
database = "chap_#{env}"

task :connect_db => [:environment] do
  ActiveRecord::Base.establish_connection(
    :adapter => 'mysql',
    :host => 'localhost',
    :username => 'root',
    :database => 'mysql',
    :socket => '/opt/local/var/run/mysql5/mysqld.sock'
   )
end

task :setup_db => [:connect_db] do
  ActiveRecord::Schema.define do
    create_database database
  end
end

task :destroy_db => [:connect_db] do
  ActiveRecord::Schema.define do
    drop_database database
  end
end

データベースを作成する。

$ rake setup_db
(in /Users/monote/dev/rails/chap0425)
-- create_database("chap_development")
   -> 0.0453s
monote$ 

モデルを作成する。

$ ruby script/generate model member

データベースの要素を作成する。

/db/migrate/001_create_members.rb

class CreateMembers < ActiveRecord::Migration
  def self.up
    create_table :members do |t|
      t.column :text, :string
      t.column :create_at, :datetime      
    end
  end

  def self.down
    drop_table :members
  end
end

テーブル/項目作成。

$ rake db:migrate
(in /Users/monote/dev/rails/chap0425)
== CreateMembers: migrating ===================================================
-- create_table(:members)
   -> 0.0027s
== CreateMembers: migrated (0.0030s) ==========================================

スキャッフォールド作成。

$ ruby script/generate scaffold Member main
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/main
      exists  app/views/layouts/
      exists  test/functional/
  dependency  model
:0:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
   identical    app/models/member.rb
   identical    test/unit/member_test.rb
   identical    test/fixtures/members.yml
      create  app/views/main/_form.rhtml
      create  app/views/main/list.rhtml
      create  app/views/main/show.rhtml
      create  app/views/main/new.rhtml
      create  app/views/main/edit.rhtml
      create  app/controllers/main_controller.rb
      create  test/functional/main_controller_test.rb
      create  app/helpers/main_helper.rb
      create  app/views/layouts/main.rhtml
      create  public/stylesheets/scaffold.css

これで完成。
エラーが表示されるようだったら、再起動。

gettext-01インストール

gettextとは、ソフトウェアの他言語対応のためのライブラリー

$gem install gettext
$ gem install gettext
WARNING:  Installing to ~/.gem since /opt/local/lib/ruby/gems/1.8 and
	  /opt/local/bin aren't both writable.
WARNING:  You don't have /Users/monote/.gem/ruby/1.8/bin in your PATH,
	  gem executables will not run.
Successfully installed locale-2.0.1
Successfully installed gettext-2.0.1
2 gems installed
Installing ri documentation for locale-2.0.1...
Installing ri documentation for gettext-2.0.1...
Installing RDoc documentation for locale-2.0.1...
Installing RDoc documentation for gettext-2.0.1...

scaffold-02

databaseの状態。

mysql> show tables;
+--------------------------------+
| Tables_in_chap0424_development |
+--------------------------------+
| members                        | 
| schema_info                    | 
+--------------------------------+
2 rows in set (0.00 sec)

mysql> show fields from members;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name  | varchar(255) | YES  |     | NULL    |                | 
| email | varchar(255) | YES  |     | NULL    |                | 
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

■scaffold

$ ruby script/generate scaffold member main

http://0.0.0.0:3001/main/

scaffold-01作成

/config/database.yml

development:
  adapter: mysql
  database: member_development
  username: root
  password:
  host: localhost
test:
  adapter: mysql
  database: member_test
  username: root
  password:
  host: localhost

production:
  adapter: mysql
  database: member_production
  username: root
  password: 
  host: localhost

■データベース作成。

mysql>create database member0420_development character set utf8;

■モデルの作成

$ ruby script/generate model member
./script/../config/boot.rb:20:Warning: Gem::SourceIndex#search support for String patterns is deprecated
:0:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/member.rb
      create  test/unit/member_test.rb
      create  test/fixtures/members.yml
      create  db/migrate
      create  db/migrate/001_create_members.rb

■/db/migrate/001_create_members.rb

class CreateMembers < ActiveRecord::Migration
  def self.up
    create_table :members do |t|
      t.column :number, :integer
      t.column :name, :string
      t.column :create, :datetime
    end
  end

  def self.down
    drop_table :members
  end
end

■migrate(テーブル作成、field作成。)

$ rake db:migrate
(in /Users/mono/dev/rails/chap0424)
== CreateMembers: migrating ===================================================
-- create_table(:members)
   -> 0.0034s
== CreateMembers: migrated (0.0036s) ==========================================

■scaffold

$ ruby script/generate scaffold Member
./script/../config/boot.rb:20:Warning: Gem::SourceIndex#search support for String patterns is deprecated
:0:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/members
      exists  app/views/layouts/
      exists  test/functional/
  dependency  model
:0:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
   identical    app/models/member.rb
   identical    test/unit/member_test.rb
   identical    test/fixtures/members.yml
      create  app/views/members/_form.rhtml
      create  app/views/members/list.rhtml
      create  app/views/members/show.rhtml
      create  app/views/members/new.rhtml
      create  app/views/members/edit.rhtml
      create  app/controllers/members_controller.rb
      create  test/functional/members_controller_test.rb
      create  app/helpers/members_helper.rb
      create  app/views/layouts/members.rhtml
      create  public/stylesheets/scaffold.css

http://0.0.0.0:3001/members

Scaffold-00補足

■参照
http://d.hatena.ne.jp/monote/20090404/1238848153

■なぜか再起動するとPATHが通っていないので

$export PATH=$PATH:/opt/local/lib/mysql5/bin

mysqlログイン

$mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)

エラーが表示される。いろいろやったが、拉致があがらないので、

■初期化

$ sudo -u mysql mysql_install_db5
Password:
Installing MySQL system tables...
090423 10:29:45 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090423 10:29:45 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090423 10:29:45 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK
Filling help tables...
090423 10:29:45 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090423 10:29:45 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090423 10:29:45 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h mono-no-macbook.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com

■起動

$sudo /opt/local/share/mysql5/mysql/mysql.server start

■エラーになる。

$ mysql5 -u root -pEnter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

■ログイン

$ mysql5 -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>