Ruby On Rails 学习笔记(3)

作者:

周五,和RoR学习会小组其他成员,一起组织了关于RoR的发表会。我承担的是关于自动测试,以及RoR和Agile的结合运用。因为只学了不到一个月,而且我的发表时间在20分钟左右。所以,尽量先入为主的方式发表。也没有准备PPT的演示。

1)向大家确定说明的内容和时间

1. 从0开始做成一个Product的CRUD画面 (5minutes – 5 minutes)
2. 讨论关于快速开发的特征 (5m)
3. 讨论测试在快速开发中的作用 (3m)
4. 讨论RoR实现自动测试的方式(回顾之前的程序)(5m)
5. RoR对Agile的支持和今后的研究 (2m)
Totoal:20 minutes

2)准备Teminal命令窗口,Safari窗口,以及纪录命令的Evernote。

告诉大家和一个Java Web开发比较,这个又多干净。

3)执行命令,快速建立一个包含所有CRUD的画面和自动测试的实际应用

1.建立第一个应用

[Step1]

# $ rails new dongdong
$ rails new dongdong --skip-bundle
$ cd dongdong
$ bundle install --local

[Step2]
rails generate scaffold Product title:string description:text image_url:string price:decimal

[Step3]
$ cd db/migrate/
$ vi 20120118151020_create_products.rb
t.decimal :price, :precision => 8, :scale =>2
$ cd ../..
$ rake db:migrate

[Step4]
$ cd Desktop/dongdong/
$ rails server

[Step5]
http://localhost:3000/products

2. Load一些测试数据,稍微美化一下

[Step1]
$ cp ../depot/db/seens.rb db/
$ cp -R ../depot/public/images public/
$ rake db:seed

[Step2]
cp ../depot/app/views/products/index.html.erb app/views/products/
cp ../depot/app/assets/stylesheets/depot.css.scss app/assets/stylesheets/

3. 加入Validate
[Step1]
$ rake test
我们再Apatana里面建立一个Project

[Step2]
$ vi app/models/product.rb

validates :title, :description, :image_url, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, :format =>{
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}

[Step3]
$ rake test

4. 我们哪边出错了啊
$ vi test/functional/products_controller_test.rb

@update ={
:title => 'Lorem Ipsum',
:description => 'Wibbles are fun!',
:image_url => 'lorem.jpg',
:price => 19.95
}

$rake test

5. 解决了错误,完了么,我们还要测试
vi test/unit/product_test.rb 
[Step1] 输入空值该出错啊
  test "product attributes must not be empty"do
    product = Product.new
    assert product.invalid?
    assert product.errors[:title].any?
    assert product.errors[:description].any?
    assert product.errors[:price].any?
    assert product.errors[:image_url].any?
  end

[Step2]输入不符合要求的数据也该出错吧
test "product price must be positive"do
    product = Product.new(:title       => "My Book Title",
                          :description => "yyy",
                          :image_url   => "zzz.jpg")
    product.price = -1
    assert product.invalid?
    assert_equal "must be greater than or equal to 0.01", 
      product.errors[:price].join('; ')

    product.price = 0
    assert product.invalid?
    assert_equal "must be greater than or equal to 0.01", 
      product.errors[:price].join('; ')

    product.price = 1
    assert product.valid?
  end

[Step3]书名不应该重复啊,可以怎么测试呢
比需要有背景数据
/test/fixtures/products.yml
ruby: 
  title:      Programming Ruby 1.9
  description: 
    Ruby is the fastest growing and most exciting dynamic
    language out there.  If you need to get working programs
    delivered fast, you should add Ruby to your toolbox.
  price:      49.50
  image_url:  ruby.png 

  fixtures :products

  test "product is not valid without a unique title"do
    product = Product.new(:title       => products(:ruby).title,
                          :description => "yyy", 
                          :price       => 1, 
                          :image_url   => "fred.gif")

    assert !product.save
    assert_equal "has already been taken", product.errors[:title].join('; ')
  end

4)讨论关于快速开发的基本特征


5)最后在讨论的基础上回答大家的问题。

20分钟搞定。接下来还要更深入地理解RoR。