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. python+Django框架运用(一)

    Django 介绍: django是一个采用Python语言开发的开源框架,2005年发布.早期是做新闻以及内容管理的网站的,提供了非常强大的后管理系统. django官网:https://www.d ...

  2. VNC的安装和常用命令

    主要参考文章:http://www.cnblogs.com/coderzh/archive/2008/07/16/1243990.html                         http:/ ...

  3. web前端----html基础

    一.初始html 1.web服务本质 import socket sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.bind((&q ...

  4. django 项目中使用多数据库 multiple databases

    假如在一个django项目中使用到了不只一个数据库, 其实这在大一点的工程中很常见,比如主从库 那么会涉及到如下一些东西 1, 定义 在settings中的DATABASE中定义会使用到的数据,比如除 ...

  5. 20145333茹翔《网络对抗》Exp9 Web安全基础实践

    20145333茹翔<网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入原理,如何防御 SQL注入 就是通过把SQL命令插入到"Web表单递交"或&q ...

  6. 20165211 2017-2018-2 《Java程序设计》第6周学习总结

    20165211 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 本周,我学习了书本上第八.十五两章的内容,以下是我整理的主要知识. 第八章 常用实用类 St ...

  7. linux内核分析 第5章读书笔记

    第五章 系统调用 一.与内核通信 系统调用在用户控件进程和硬件设备之间添加了一个中间层,作用有: 为用户空间提供了一种硬件的抽象接口 系统调用保证了系统的稳定和安全 每个进程都运行在虚拟系统中,而在用 ...

  8. Tensorflow游乐场

    昨天,Google发布了Tensorflow游乐场.Tensorflow是Google今年推出的机器学习开源平台.而有了Tensorflow游乐场,我们在浏览器中就可以训练自己的神经网络,还有酷酷的图 ...

  9. WIFI模块对比介绍

    一.ESP8266(官网 https://espressif.com/)1 简介 乐鑫智能互联平台——ESCP 拥有高性能无线SOC,给移动平台设计师带来福音,它 以最低成本提供最大实用性,为WiFi ...

  10. noip 邮票面值设计 - 搜索 - 动态规划

    描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定M(N+M<=10)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大max ,使得1-max之间的每一个邮资值都能 ...