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...
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]
September 7th, 2010 at 10:03 PM
Buy:Actos.Prednisolone.Arimidex.Accutane.Human Growth Hormone.Lumigan.Zovirax.Valtrex.Zyban.Synthroid.Retin-A.Mega Hoodia.Nexium.Petcam (Metacam) Oral Suspension.100% Pure Okinawan Coral Calcium.Prevacid….
December 23rd, 2010 at 2:57 AM
dosage of amoxicillin for respiratory disorder…
Buygeneric drugs…
December 24th, 2010 at 3:45 PM
Cialis discount…
Buyno prescription…
December 30th, 2010 at 10:12 AM
Amoxicillin@official.site” rel=”nofollow”>……
Buyno prescription…
December 31st, 2010 at 10:21 PM
Azor@official.site” rel=”nofollow”>……
Buygeneric pills…
January 2nd, 2011 at 1:47 PM
Crestor@official.site” rel=”nofollow”>.…
Buywithout prescription…
January 5th, 2011 at 2:11 AM
generic@viagra.sildenafil.paypal.no.prescription” rel=”nofollow”>……
Buygeneric meds…
January 6th, 2011 at 4:15 AM
abilify@and.sleep.issues” rel=”nofollow”>.…
Buyno prescription…
January 12th, 2011 at 3:09 PM
purchase@effexor.now” rel=”nofollow”>.…
Buyno prescription…
January 21st, 2011 at 6:20 PM
Depakote@official.site” rel=”nofollow”>..…
Buygeneric meds…
February 4th, 2011 at 12:30 AM
Zyvox…
Buydrugs without prescription…
February 4th, 2011 at 5:32 AM
Zyrtec…
Buydrugs without prescription…
February 4th, 2011 at 11:49 AM
xeloda…
Buynow…
February 4th, 2011 at 4:05 PM
xeloda bleeding…
Buygeneric meds…
February 5th, 2011 at 3:15 AM
what is the shelf life of zyrtec…
Buynow it…
February 5th, 2011 at 8:25 AM
Zoloft…
Buygeneric drugs…
February 6th, 2011 at 11:52 AM
Voltaren…
Buyno prescription…
February 7th, 2011 at 4:49 PM
Xenical…
Buynow…
March 3rd, 2011 at 5:43 PM
Zofran…
Buygeneric pills…
March 5th, 2011 at 7:14 PM
pyrantel pamoate gets rid of which worms…
Buydrugs without prescription…
March 7th, 2011 at 12:08 PM
Orlistat…
Buyit now…
March 8th, 2011 at 1:54 AM
Synthroid…
Buygeneric pills…
March 8th, 2011 at 9:21 PM
Prozac…
Buyno prescription…
March 11th, 2011 at 3:01 AM
Rogaine…
Buygeneric meds…
March 29th, 2011 at 1:00 PM
Zoloft…
Buynow it…
March 31st, 2011 at 11:21 AM
Provera@Provera.Provera” rel=”nofollow”>.…
Buygeneric pills…
April 1st, 2011 at 12:19 AM
Cipro@Cipro.Cipro” rel=”nofollow”>..…
Buygeneric meds…
April 1st, 2011 at 10:07 PM
pediatric@griseofulvin.liver.tumors” rel=”nofollow”>……
Buygeneric pills…
April 2nd, 2011 at 1:17 AM
do@you.need.to.fast.for.prograf.levels” rel=”nofollow”>……
Buywithout prescription…
April 2nd, 2011 at 2:49 AM
.…
Buydrugs without prescription…
April 2nd, 2011 at 8:47 AM
cms@coverage.of.pulmicort.respules.j7627” rel=”nofollow”>.…
Buygeneric pills…
April 2nd, 2011 at 1:11 PM
dapple@hydrocortisone.cream” rel=”nofollow”>……
Buyit now…
April 2nd, 2011 at 7:46 PM
purpose@of.advair” rel=”nofollow”>.…
Buygeneric meds…
April 3rd, 2011 at 2:11 AM
if@you.are.allergic.to.ceclor.can.you.take.augmentin” rel=”nofollow”>……
Buywithout prescription…
April 3rd, 2011 at 7:51 PM
bulk@coral.calcium.powder” rel=”nofollow”>..…
Buygeneric drugs…
April 3rd, 2011 at 9:29 PM
buy@acai.berry.supplement” rel=”nofollow”>.…
Buyit now…
April 3rd, 2011 at 10:54 PM
accupril@side.affects” rel=”nofollow”>……
Buyno prescription…
April 3rd, 2011 at 11:48 PM
accutane@and.ototoxicity” rel=”nofollow”>……
Buyno prescription…
April 4th, 2011 at 12:41 AM
does@clonidine.and.lamisil.interaction” rel=”nofollow”>..…
Buywithout prescription…
April 4th, 2011 at 4:31 AM
sexual@side.effects.of.lexapro.vs.paxil” rel=”nofollow”>..…
Buynow it…
April 4th, 2011 at 8:19 AM
aciphex@vs.nexium” rel=”nofollow”>..…
Buygeneric drugs…
April 4th, 2011 at 6:05 PM
.…
Buyit now…
April 5th, 2011 at 1:26 PM
pulmicort@or.xopenex.first.when.using.both” rel=”nofollow”>……
Buyit now…
April 5th, 2011 at 4:52 PM
.…
Buygeneric drugs…
April 6th, 2011 at 1:22 AM
coral@calcium.safety” rel=”nofollow”>.…
Buyit now…
April 6th, 2011 at 7:18 PM
flomax@in.dogs” rel=”nofollow”>.…
Buyit now…
April 6th, 2011 at 11:08 PM
calories@and.nutrition.in.boost.nutritional.energy.drink” rel=”nofollow”>……
Buywithout prescription…
April 7th, 2011 at 5:24 PM
multiple@myeloma.gleevec.clinical.trials” rel=”nofollow”>.…
Buynow it…
April 7th, 2011 at 7:07 PM
..…
Buynow it…