shoulda on rails
Posted by HoLin on January 7th, 2009 filed in Rails应用, 敏捷开发, 测试方法在新项目中配置shoulda
1 2 3 4 5 6 7 | rails shoulda_demo -d mysql cd shoulda_demo/ script/plugin install git://github.com/thoughtbot/shoulda.git sudo gem install thoughtbot-factory_girl --source http://gems.github.com script/plugin install git://github.com/technicalpickles/factory_girl_on_rails.git script/plugin install git://github.com/hardbap/coulda.git |
测试model
生成model
1 | script/generate coulda_model subject |
写测试代码,编辑文件 test/unit/subject_test.rb
1 2 3 4 5 6 | require File.dirname(__FILE__) + '/../test_helper' class SubjectTest < ActiveSupport::TestCase should_have_many :questions should_require_attributes :name end |
运行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | HoLin:tiku holin$ rake test (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/subject_test.rb" "test/unit/user_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started FF............. Finished in 0.301899 seconds. 1) Failure: test: Subject should have many questions. (SubjectTest) ...: Subject does not have any relationship to questions. <nil> is not true. 2) Failure: test: Subject should require name to be set. (SubjectTest) ...: Subject allowed nil as a value for name. <false> is not true. 15 tests, 28 assertions, 2 failures, 0 errors /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ............... Finished in 0.434793 seconds. 15 tests, 27 assertions, 0 failures, 0 errors /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" Errors running test:units! |
出错,编辑文件 app/models/subject.rb 来让测试通过
1 2 3 4 | class Subject < ActiveRecord::Base has_many :questions validates_presence_of :name end |
再次运行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | HoLin:tiku holin$ rake test (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/subject_test.rb" "test/unit/user_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started E.............. Finished in 0.333493 seconds. 1) Error: test: Subject should have many questions. (SubjectTest): NameError: uninitialized constant Question ... 15 tests, 31 assertions, 0 failures, 1 errors /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ............... Finished in 0.460992 seconds. 15 tests, 27 assertions, 0 failures, 0 errors /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" Errors running test:units! |
还是通不过,新建question model
1 2 3 4 5 6 7 8 9 | HoLin:tiku holin$ script/generate coulda_model question exists app/models/ exists test/unit/ exists test/factories/ create app/models/question.rb create test/unit/question_test.rb create test/factories/question_factory.rb exists db/migrate create db/migrate/20090107064242_create_questions.rb |
运行测试
1 2 3 4 5 | HoLin:tiku holin$ rake test (in /Users/holin/work/kuxuesoft/tiku) You have 1 pending migrations: 20090107064242 CreateQuestions Run "rake db:migrate" to update your database then try again. |
需要先完成所有的Migration,那就完成他们吧。
1 2 3 4 5 6 | HoLin:tiku holin$ rake db:migrate (in /Users/holin/work/kuxuesoft/tiku) == CreateQuestions: migrating ================================================ -- create_table(:questions) -> 0.0347s == CreateQuestions: migrated (0.0351s) ======================================= |
再运行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | HoLin:tiku holin$ rake test (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/question_test.rb" "test/unit/subject_test.rb" "test/unit/user_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ............... Finished in 0.323642 seconds. 15 tests, 32 assertions, 0 failures, 0 errors /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ............... Finished in 0.438684 seconds. 15 tests, 27 assertions, 0 failures, 0 errors /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" |
通过测试
测试controller
生成controller
1 2 3 4 5 6 7 8 | HoLin:tiku holin$ script/generate controller subjects exists app/controllers/ exists app/helpers/ create app/views/subjects exists test/functional/ create app/controllers/subjects_controller.rb create test/functional/subjects_controller_test.rb create app/helpers/subjects_helper.rb |
编写测试代码,编辑文件test/functional/subjects_controller_test.rb
1 2 3 4 5 6 7 8 9 10 | require 'test_helper' class SubjectsControllerTest < ActionController::TestCase context "on POST to :create" do setup { post :create, :subject => {:name => 'CET-6', :desc => 'description goes here' } } should_assign_to :subject should_redirect_to "subject_url(@subject)" end end |
运行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | HoLin:tiku holin$ rake test:functionals (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/subjects_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ..........FE..... Finished in 0.603312 seconds. 1) Failure: test: on POST to :create should assign @subject. (SubjectsControllerTest) [...]: The action isn't assigning to @subject. <nil> expected to not be nil. 2) Error: test: on POST to :create should redirect to "subject_url(@subject)". (SubjectsControllerTest): NoMethodError: undefined method `subject_url' for #<SubjectsControllerTest:0x227f428> ... 17 tests, 28 assertions, 1 failures, 1 errors rake aborted! Command failed with status (1): [/usr/local/bin/ruby -Ilib:test "/usr/local...] (See full trace by running task with --trace) |
添加routes
1 | map.resources :subjects |
运行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | HoLin:tiku holin$ rake test:functionals (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/subjects_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ..........FE..... Finished in 0.536594 seconds. 1) Failure: test: on POST to :create should assign @subject. (SubjectsControllerTest) [...]: The action isn't assigning to @subject. <nil> expected to not be nil. 2) Error: test: on POST to :create should redirect to "subject_url(@subject)". (SubjectsControllerTest): ActionController::RoutingError: subject_url failed to generate from {:controller=>"subjects", :action=>"show", :id=>nil}, expected: {:controller=>"subjects", :action=>"show"}, diff: {:id=>nil} ... 17 tests, 28 assertions, 1 failures, 1 errors rake aborted! Command failed with status (1): [/usr/local/bin/ruby -Ilib:test "/usr/local...] (See full trace by running task with --trace) |
编辑app/controllers/subjects_controller.rb, 增加create action
1 2 3 4 5 6 7 8 9 10 11 12 13 | class SubjectsController < ApplicationController def create @subject = Subject.new(params[:subject]) if @subject.valid? && @subject.save flash[:message] = "添加成功" redirect_to subject_url(@subject) else render :action => 'new' end end end |
运行测试
1 2 3 4 5 6 7 8 9 | HoLin:tiku holin$ rake test:functionals (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/subjects_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ................. Finished in 0.687698 seconds. 17 tests, 29 assertions, 0 failures, 0 errors |
测试通过
测试需要登陆的操作、嵌套context
编写测试代码,编辑文件test/functional/subjects_controller_test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | require 'test_helper' class SubjectsControllerTest < ActionController::TestCase context "do these after logged in" do setup { @request.session[:user_id] = User.first.id } context "on POST to :create" do setup { post :create, :subject => {:name => 'CET-6', :desc => 'description goes here' } } should_assign_to :subject should_redirect_to "subject_url(@subject)" end context "on POST to :create without subject name" do setup { post :create, :subject => {:desc => 'description goes here' } } should_assign_to :subject should_redirect_to "new_subject_url" end end end |
运行测试
1 2 3 4 5 6 7 8 9 | HoLin:tiku holin$ rake test:functionals (in /Users/holin/work/kuxuesoft/tiku) /usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/home_controller_test.rb" "test/functional/sessions_controller_test.rb" "test/functional/subjects_controller_test.rb" "test/functional/users_controller_test.rb" Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader Started ................... Finished in 0.572731 seconds. 19 tests, 31 assertions, 0 failures, 0 errors |
参考资源
Related posts:
- Ruby 1.8.7, Rails 2.1.0 API 文档 假定rdoc安装在:/usr/local/lib/ruby/1.8/rdoc 1,下载并解压jaxdoc(http://rubyforge.org/projects/jaxdoc/) sudo cp ajax_generator.rb /usr/local/lib/ruby/1.8/rdoc/generators sudo cp -R ajax...
- Nginx + Passenger 开发Rails应用 在Rails开发过程中,如果同时开发多个项目,需要经常切换,使用 script/server 去启动应用会比较麻烦,而采用nginx(或apache) + passenger可以节约时间,提高效率。 1、安装passenger 下载Passenger,目前最新的版本为2.2.2,并解压到passenger的安装目录: tar xzvf passenger-2.2.2.tar.gz...
- Satellite: a self-syncing distributed wiki 介绍 satellite is a self-syncing distributed wiki with file uploads...
- Rspec 基础 gem方式安装(spec example.rb) gem install rspec plugin方式安装(script/spec example.rb) ruby script/plugin install...
- Nagios中用飞信发送报警信息 近期由于飞信的API变更,原先的Linux版本fetion程序不能再用来发送信息了。而由 flyerhzm 开发的rfetion更新较快,可用来配合Nagios的报警信息发送。 rfetion是基于ruby的一个飞信客户端工具,有命令行的发送模式。使用前,需事先安装好 ruby,及rubygem,相关安装过程在网上有很多。 安装好ruby及rubygem后,可通过如下两行命令来安装rfetion: gem sources -a http://gemcutter.org...
Related posts brought to you by Yet Another Related Posts Plugin.
January 7th, 2009 at 7:35 PM
好东东,学习了
[Reply]
January 8th, 2009 at 9:30 PM
怎么和zentest集成 ?
[Reply]