在新项目中配置shoulda
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
script/generate coulda_model subject
写测试代码,编辑文件 test/unit/subject_test.rb
require File.dirname(__FILE__) + '/../test_helper'
class SubjectTest < ActiveSupport::TestCase
should_have_many :questions
should_require_attributes :name
end
运行测试
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 来让测试通过
class Subject < ActiveRecord::Base
has_many :questions
validates_presence_of :name
end
再次运行测试
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
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
运行测试
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,那就完成他们吧。
HoLin:tiku holin$ rake db:migrate
(in /Users/holin/work/kuxuesoft/tiku)
== CreateQuestions: migrating ================================================
-- create_table(:questions)
-> 0.0347s
== CreateQuestions: migrated (0.0351s) =======================================
再运行测试
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
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
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
运行测试
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
map.resources :subjects
运行测试
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
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
运行测试
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
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
运行测试
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
参考资源:http://thoughtbot.com/projects/shoulda/tutorial
http://alexbrie.net/1526/functional-tests-with-login-in-rails/
分享到:
相关推荐
另外,测试部分引入了Shoulda和Factory Girl等测试工具,它们可以帮助开发者编写更简洁、更高效的测试用例。 Rails 3.1还引入了CoffeeScript作为默认的JavaScript语言,这是一种语法糖,它可以编译成标准的...
8. **Testing**:Rails 3增强了测试框架,支持Shoulda、Factory Girl等库,使得编写测试用例更加高效。同时,Test::Unit和RSpec都得到了改进,提供了一流的测试体验。 9. **Internationalization (i18n)**:Rails 3...
Shoulda 是一个 Ruby on Rails 的测试插件,它让 Test::Unit 测试框架具备了行为驱动开发(Behavior Driven Development, BDD)的特性。通过使用 Shoulda,开发者能够以更自然、更易于阅读的方式来编写测试代码。 #...
You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...
建于Ruby3 Ruby on Rails 6 Shoulda Matchers水豚规格入门要启动并运行本地副本,请遵循以下简单的示例步骤。先决条件Ruby on Rails v6.x。有关如何安装Ruby on Rails的更多信息,请单击此设置和安装使用上面的链接...
实体关系图建于Ruby v2.7.0 Ruby on Rails PostgreSQL设计引导程序HTML CSS经过测试Ruby gem RSpec Ruby宝石Shoulda Matchers Ruby宝石水豚现场演示打开先决条件Ruby:2.6.3 滑轨:5.2.3 Postgres:> = 9.5 纱文本...
目录 ...这是什么? 越来越多的资源集中在Ruby on Rails及其方面方面,分为各种类别 我会尽力维护此存储库,但是如果您发现不良的掉毛或未完成/列出的资源,请随时做出贡献。 原子 ...Shoulda Matchers
Peter Armstrong is the co-founder and CEO of Ruboss Technology Corporation, a Vancouver, BC area company focusing on Adobe Flex and Ruby on Rails development and consulting. He is the co-creator of ...
01:00PM Ruby on Rails: Why We Should Move On 60min 02:00PM Common Ruby Errors 45min 02:45PM Pair Programming vs Noise 45min 03:30PM Programming in the Boondocks of Seattle 30min 04:00PM Ruby vs. ...
One such tool is `sap4rails`, which aims to provide developers with a streamlined way to interact between SAP systems and Ruby on Rails applications. This article delves into the details of ...
Ruby on Rails中的Internet DEMO商店实施符合以下要求的Internet商店DEMO: User authentication and authorization;User roles (guest, registered user, admin);Product categories menu;Product cart available ...
庇护所礼物-一款易于将需要的物品直接捐赠给全国各地无家可归者庇护所的应用程序! 描述 庇护所礼品使游客可以轻松浏览全国各地庇护所所需的精选物品清单。 物品清单直接来自各个庇护所; 特别是来自他们的亚马逊...
RSpec Rails示例 一个以Rails应用程序形式的RSpec速查表。 了解如何从模型代码库中专业测试Rails应用程序对于那些想知道如何使用RSpec测试Rails应用程序的开发人员来说,这是一个简短而全面的参考。 在这里,您将...
Struts, Java Server Faces, Ruby on Rails or something else. These tools can save you a lot of work, but you will have to integrate them, and there is no question that a non-trivial site with ...
#iCook 能做什么 GA WDI DC 2014 年 8 月,项目 3... 用于测试的 shoulda-matchers gem PostgreSQL 数据库 设计 gem 进行身份验证和授权 Autoprefixer gem 使 CSS 动画工作 Javascript 使用 CSS3 和 HTML5 自定义样式
Shoulda(用于测试的Ruby宝石) Devise(处理用户模型构建的Ruby gem,为用户控制器路由 Poltergeist(基于PhantomJS的Capybara的无头浏览器(用于自动进行网页交互的脚本化无头浏览器;提供了JavaScript API,可...
MSYS2 is required to build native C/C++ extensions for Ruby and is necessary for Ruby on Rails. Moreover it allows the download and usage of hundreds of Open Source libraries which Ruby gems can ...
Ruby on Rails 339 Restlet 343 Django 354 A. Some Resources for REST and Some RESTful Resources.. . . . 365 Standards and Guides 365 Services You Can Use 367 B. The ...