validates处理验证错误:详见ActiveModel::Errors文档

一,errors

ActiveModel::Errors的实例包含所有的❌。每个错误:key是每个属性的name, value是一个数组,包含错误消息string.

例子:

person = Person.new

person.errors.messages   #=> {:name => ["can't be blank", "is too short"], ...}

二 ,errors[]

通过key获取value ,如person.errors[:name]  #=> ["can't be blank", "is too short"]

三, errors.add(atr, msg) 或者 errors.message[atr] << "msg"

手动添加某属性的错误message

errors.full_messages: 友好显示所有错误message, 用嵌套的hash显示。

add(attribute, message=:invalid, options={})

添加message给errors, 同时可以使用validator type 来在attribute上显示details

同一个attributes可以同时添加多个error。

如果没有自定义message ,默认使用:invalid。

 > p.errors.add(:title, :not_implemente, message: "must be implemented")
 => ["must be implemented"]
 > p.errors.messages
 => {:title=>["must be implemented"]}
 > p.errors.details
 => {:title=>[{:error=>:not_implemente}]}

分析: :not_implemente是自定义的验证类型,对应其message。使用p.errors.details,得到的是属性和属性指向的错误类型,

四, errors.details,

在add()方法内增加限制参数,not_allowed: "xxx"

五, errors[:base] << "string"

如果error不是直接管理到具体一个attribute上,可以使用:base .

把错误message添加到整个对象上。不是针对属性。不管什么错误,只像把对象标记为无效,就使用这个方法。

六,errors.clear 和 errors.empty?

清除errors 集合中所有message, errors.empty?查看是否错误集合是空的,配合clear使用。

七,errors.size,返回错误消息总数。 等同errors.count

八,视图上显示错误消息。

可以使用scarfold,在_form.html.erbzhong 自动加入ERB代码。或者自己写。


validate(*args, &block) #用于 自定义的验证方法

validates(*attributes)  #用于验证表格的属性。这里有s


Rspec的方法:

to_not , be_valid


Rails程序开发,时间区默认是UTC,需要改为"Beijing"

在config/application.rb中,类Application中,加config.time_zone = "Beijing"


Float#ceil,返回大于等于某个数字的最小的数字。参数指小数点后的位数。
1.2.ceil      #=> 2
2.0.ceil #=> 2
(-1.2).ceil #=> -1
(-2.0).ceil #=> -2
1.234567.ceil(3)   #=> 1.235

TDD(Test-Driven Development)

测试驱动开发:

  1. 刚开始写测试的时候,关键要素,是测什么,要测试哪些案例example才算程序正确,可以列表一个清单。
  2. 先看到测试失败,才表示测试有作用。因此,可以先写测试代码,再写实做代码。
  3. 不要一次性把全部测试example写完,而是新写一段测试代码,让测试失败,再改正实做让实做通过,最后改进这个代码refactor重构。反复这个步骤,完成全部测试example。


测试代码比实做代码多是正常的,但很简单

  1. 建立测试资料
  2. 执行程序
  3. 检查结构-> TDD

如何在测试中除错

1利用puts输出信息

2关注需要除错的example

  • 方法1: 把其他案例注释掉
  • 方法2:编辑spec/rails_helper.rb,加上两行设定
spec/rails_helper.rb
  RSpec.configure do |config|
# (略) + config.filter_run :focus => true
+ config.run_all_when_everything_filtered = true

然后修改 spec/models/parking_spec.rb 针对你想要单独测试的案例it/或者一组案例context,加上 :focus => true,例子:

it "30 mins should be ¥2", :focus => true do...end

3.使用内置gem 'byebug'。 平时开发除错也可以使用。

app/models/parking.rb
   def calculate_amount 
+    byebug      # (略)

执行测试会在中途停顿,并告知上下文环境。输入最后输入 continue 就会继续执行下去


Float@truncate: 返回要求的位数,默认只返回整数。

%:整除。



Feature Spec 验收测试

需要用到gem 'capybara' 水豚的意思。

Key benefits:

  • works out of the box for Rails。开箱即用
  • Intuitive API: mimics the language an actual user would use. 直观直觉API

Setup:

gem 'capybara'

Using Capybara with RSpec

在spec/rails_helper.rb添加(或者其他test helper file):

require 'capybara/rails'

require 'capybara/rspec'

⚠️: rails_helper已经require了文件spec_helper。所以require 'capybara/rspec'可以放到这两个文件任意一个中。

问题 :type => :feature 到底加不加,有啥用?

文档:

如果用Rails,并且Capybara specs文件在其他目录(非spec/features,or spec/system), 需给example groups加上tag, type: :feature or type: :system

问题:如果测试JavaScript。

使用js:true 来打开Capybara.javascript_dirver(默认:selenium)(具体看文档说明,有点复杂)

describe 'some stuff which requires js', js: true do
it 'will use the default js driver'
# it 'will switch to one specific driver', driver: :webkit
end

执行 mkdir spec/features 建立验收测试的目录

新增 spec/features/guest_spec.rb

require 'rails_helper'
feature "parking", :type => :feature do
  scenario "guest parking" do
    visit "/"   #浏览首页

save_and_open_page

    expect(page).to have_content("一般费率") #检查HTML中应出现"一般费率"
    click_button "开始计费" #按键
    click_button "结束计费"
    expect(page).to have_content("2.00")
  end
end

解释:

  1. feature 是别名: describescenario (a written outline of what happens in a film/movie or play 剧情概要。)等同于 it
  2. 其他别名:background: before, given: let
  3. 这里 have_content 来检查指定文字有没有出现在 HTML 里面。Web 应用的测试,没办法去完整比对 HTML 字串,因为字串比对差一个空白就不一样。如果说设计师稍微多加一个 <br> 就要改测试,这样就太累了,测试会太敏感。所以我们只能检查说 HTML 里面有出现我们希望要有的关键字。
  4. 注意到我们不需要再重复测试金额对不对了。Feature Spec 的重点在于检查东西(Model + Controller + View)接起来有没有正常运作。
  5. 被注解掉的 save_and_open_page 会存下测试当时的 HTML 页面(snapshot网页块照),除错的时候可以使用。如果打开的话,跑测试会出现:
File saved to /Users/chentianwei/离线保存的全栈文件/我的练习/rails自动化测试/parking-app/tmp/capybara/capybara-201805142036353571416628.html.

    within("#new_user") do  # 填表单,模拟用户填写表单送出的情况。
fill_in "Email", with: "foobar@example.com"
fill_in "Password", with: "12345678"
fill_in "Password confirmation", with: "12345678"
end
   click_button "Sign up"
  expect(page).to have_content("Welcome! You have signed up successfully")

这里我们模拟用户填写表单送出的情况,用fill_in可以填入值

测试短期费率流出

Devise 提供测试用的 sign_in 方法,请改 spec/rails_helper.rb

spec/rails_helper.rb
  RSpec.configure do |config|  
+ +    config.include Devise::Test::ControllerHelpers, type: :controller 
+    config.include Devise::Test::ControllerHelpers, type: :view 
+    config.include Devise::Test::IntegrationHelpers, type: :feature 
spec/features/short_term_spec.rb
require "rails_helper"
feature "parking", :type => :feature do
  scenario "short-term parking" do
    user = User.create!(email: '1@1.com', password:'123123')
    sign_in(user)  #这样就直接登入了。
    visit '/'
    choose "短期费率" #选择radio button
    click_button "开始计费"
    click_button "结束计费"
    expect(page).to have_content("2.00")
  end
end

这里用了 choose 来对 Radio 按钮做选择,在 capybara 中还有提供其他方法针对不同表单元件做操作,例如:

  • check "核选方块名称"
  • uncheck "核选方块名称"
  • select "选项名称", :from => "下拉选单名称"
  • attach_file 上传档案

详细用法请参考 capybara 文件。

The DSL

A complete reference is available at rubydoc.info.

⚠️:默认的Capybara只会定位可见的元素,因为真实用户不会和不可见的元素互动。

pasting

Navigating

vist方法,例子:

visit "/projects"

vist (post_comments_path(@post))

have_current_path方法:当前页面又这个路径

expect(page).to have_current_path(post_comments_path(post))

Clicking links and buttons

Full reference:Capybara::Node::Actions

Capybara自动跟随redirect, submits forms 相关的buttons

click_link('Link Text')
click_button('Save')
click_on('Link Text') # clicks on either links or buttons

Interacting with forms

Full reference:Capybara::Node::Actions

fill_in('First Name', with: 'John')
fill_in('Password', with: 'Seekrit')
fill_in('Description', with: 'Really Long Text...')
choose('A Radio Button')
check('A Checkbox')
uncheck('A Checkbox')
attach_file('Image', '/path/to/image.jpg')
select('Option', from: 'Select Box')

#fill_in([locator], options = {}) ⇒ Capybara::Node::Element

定位一个text field或text area, 并填写进string, 可以通过name, id, label text来定位到这个元素。

参数:

locator(String)-- Which field  to fill in

options(Hash) (默认{})

选项:

:with(String):所要填入的值。

:id(String) -- 定位的选项。

:name(String)-- 定位的选项。

:class(String,Array<String>) -- 定位的选项。

Querying

Full reference: Capybara::Node::Matchers

一系列选项用来查询页面是否存在某个元素,操作这个元素。

You can use these with RSpec's magic matchers:

expect(page).to have_selector('table tr') 
expect(page).to have_selector(:xpath, './/table/tr')  
expect(page).to have_xpath('.//table/tr') 
expect(page).to have_css('table tr.foo') 
expect(page).to have_content('foo')

Finding

Full reference: Capybara::Node::Finders

指定寻找元素,并操作。

find_field('First Name').value
find_field(id: 'my_field').value
find_link('Hello', :visible => :all).visible?
find_link(class: ['some_class', 'some_other_class'], :visible => :all).visible? find_button('Send').click
find_button(value: '1234').click find(:xpath, ".//table/tr").click
find("#overlay").find("h1").click
all('a').each { |a| a[:href] }
find('#navigation').click_link('Home')
expect(find('#navigation')).to have_button('Sign out')

Scoping

Restrict certain actions, such as interacting with forms or clicking links and buttons, to within a specific area of the page.  within method.

within("li#employee") do
fill_in 'Name', with: 'Jimmy'
end

Restrict: ~ sth: to limit the size, amount or range of sth.

within_fieldset(), within_table()也一样。

Scripting

 在支持的drivers中,可以执行JavaScript, page.execute_script("$('body').empty()")

Modals

In drivers which support it, you can accept, dismiss and respond to alerts, confirms and prompts.

accept_alert do
click_link('Show Alert')
end

Debugging

网页快照snapshot存储当前页面在tmp/capybara/capybara-XXXxxx.html

save_and_open_page

You can also retrieve the current state of the DOM as a string using page.html.

print page.html		#在命令窗口显示html代码。

Asynchronous JavaScript (Ajax and friends)

当想要操作一个异步的JS, Capybara自动等待元素在这个页面出现。

可以调整时间,默认2秒。在时间内等待,超出则放弃或throw an error

Capybara.default_max_wait_time = 5

Capybara's waiting behaviour is quite advanced, and can deal with situations such as the following line of code:

expect(find('#sidebar').find('h1')).to have_content('Something')

5-13 Rspec实际; validates处理Errors, TDD, 单元测试和验收测试,capybara的更多相关文章

  1. 使用IdleTest进行TDD单元测试驱动开发演练(2)

    [前言] 1. 有关上篇请参见<使用IdleTest进行TDD单元测试驱动开发演练(1)>,有关本篇用到Entity Framework Code First请参见<使用NuGet助 ...

  2. TDD单元测试驱动

    使用IdleTest进行TDD单元测试驱动开发演练(2)   [前言] 1. 有关上篇请参见<使用IdleTest进行TDD单元测试驱动开发演练(1)>,有关本篇用到Entity Fram ...

  3. 使用IdleTest进行TDD单元测试驱动开发演练(3) 之 ASP.NET MVC

    一.[前言] (1)本文将用到IOC框架Unity,可参照<Unity V3 初步使用 —— 为我的.NET项目从简单三层架构转到IOC做准备>(2)本文的解决方案是基于前述<使用I ...

  4. 使用IdleTest进行TDD单元测试驱动开发演练(1)

    [前言] 开发工具:Visual Studio 2012 测试库:Visual Studio 2012自带的MSTest DI框架:Unity 数据持久层:Entity Framework 前端UI: ...

  5. C C++ TDD单元测试非常好的书

    http://product.china-pub.com/199003 测试驱动的嵌入式C语言开发 Test Driven Development for Embedded C <测试驱动的嵌入 ...

  6. 测试驱动开发(Test-Driven Development,简称TDD)--单元测试-->提高代码质量

    !!! 1.估算和做项目计划时要算上单元测试时间 2.开发之前写单元测试代码 盖房子的时候,工人师傅砌墙,会先用桩子拉上线,以使砖能够垒的笔直,因为垒砖的时候都是以这根线为基准的.TDD就像这样,先写 ...

  7. Rails 5 Test Prescriptions(everday Rspectest作者推荐) 目录 1-3章

    总文档连接: RSpec.info/documentation/ 如何使用TDD 和 自动化测试来建立一个Rails app. TDD让你用测试来探索代码的设计.你将学习可利用的工具,并学习用什么工具 ...

  8. rspec测试(使用guard自动测试和spork加速测试)配置

    Gemfile文件添加rspec.guard和spork,之后执行bundle install命令 gem 'rb-readline' group :development, :test do # C ...

  9. Oracle shutdown immediate无法关闭数据库解决方法

    在测试服务器上使用shutdown immediate命令关闭数据库时,长时间无法关闭数据库,如下所示 1: [oracle@DB-Server admin]$ sqlplus / as sysdba ...

随机推荐

  1. CQRS/ES框架调研

    1.Enode一个C#写的CQRS/ES框架,由汤雪华设计及实现,github上有相关源码,其个人博客上有详细的孵化.设计思路.版本迭代及最新的完善: 2.axon framwork,java编写,网 ...

  2. Openstack创建虚拟机 Restful api和RPC调用

    Horizon前台界面用于接受用户的输入或动作(action),然后将这些参数构造成RESTful API(https://developer.openstack.org/api-ref/comput ...

  3. Python的问题解决: IOError: [Errno 32] Broken pipe

    被该问题困扰的人还是挺多的,所以又对这个问题进行了一些更深入的分析,希望可以解决读者的问题新版本:Python 的 Broken Pipe 错误问题分析 遇到一个很奇怪的问题, web.py代码里面报 ...

  4. linux常用命令:ss 命令

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...

  5. MD5验签同一字符串得到不同的MD5签名值可能问题之一

    public static String md555(String plainText) throws UnsupportedEncodingException { byte[] secretByte ...

  6. Linux基础命令---mv

    mv 将文件或者目录移动到另一个地方,或者重命名. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法       mv [选项 ...

  7. 微服务:Java EE的拯救者还是掘墓人?

    有人认为,微服务的大行其道是在给Java EE下达死刑判决书.也有人认为,Java EE已死的论调可笑至极.读者朋友,你们怎么看? 引言 有人说,Java确实过于臃肿,经常“小题大做”.但PHP.No ...

  8. kafka调试遇到的问题

    在三台机器上以不同的端口部署了三个kafka和zookeeper实例,对应三套环境. 如: zk1:2181 zk2:2182 zk3:2183 kafka1:9092 kafka2:9093 kaf ...

  9. 20145336张子扬 《网络对抗技术》 PC平台逆向破解

    #20145336张子扬 <网络对抗技术> PC平台逆向破解 ##Shellcode注入 **基础知识** Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对 ...

  10. 在Windows下搭建Android开发环境及遇到的问题

    转载1:http://www.cnblogs.com/xdp-gacl/p/4322165.html 转载2:http://www.cnblogs.com/zoupeiyang/p/4034517.h ...