- RSpec Essentials
- Mani Tadayon
- 356字
- 2021-07-09 19:33:37
Writing specs with RSpec
We have discussed a lot of theory; now, let's start applying it. We'll write a few specs for the AddressValidator
module defined below:
module AddressValidator FIELD_NAMES = [:street, :city, :region, :postal_code, :country] VALID_VALUE = /^[A-Za-z0-9\.\# ]+$/ class << self def valid?(o) normalized = parse(o) FIELD_NAMES.all? do |k| v = normalized[k] !v.nil? && v != "" && valid_part?(v) end end def missing_parts(o) normalized = parse(o) FIELD_NAMES - normalized.keys end private def parse(o) if (o.is_a?(String)) values = o.split(",").map(&:strip) Hash[ FIELD_NAMES.zip(values) ] elseif (o.is_a?(Hash)) o else raise "Don't know how to parse #{o.class}" end end def valid_part?(value) value =~ VALID_VALUE end end end
We'll store the code above in a file called address_validator.rb
. Let's start with a couple of simple tests in this chapter. In the next chapter, we'll explore a few different ways to expand and improve these tests, but for now we'll just focus on getting up and running with our first real RSpec tests.
We'll put the following code in a file called address_validator_spec.rb
in the same folder as address_validator.rb
:
require 'rspec' require_relative 'address_validator' describe AddressValidator do it "returns false for incomplete address" do address = { street: "123 Any Street", city: "Anytown" } expect( AddressValidator.valid?(address) ).to eq(false) end it "missing_parts returns an array of missing required parts" do address = { street: "123 Any Street", city: "Anytown" } expect( AddressValidator.missing_parts(address) ).to eq([:region, :postal_code, :country]) end end
Now, let's run RSpec (make sure you have it installed already!) like this:
That's it. We used a couple of options to format the output, which is self-explanatory. We'll dig deeper into how to run specs with various options in future chapters. For now, we've accomplished our goal of running RSpec for a couple of unit tests.
Now is a good time to reflect on the concepts of testability and the unit of code. How testable is our AddressValidator
module? Do you see any potential problems? What about the units we've tested? Are they isolated and modular? Do you see any places where we could do better? Take some time to review the code and think about these questions before moving on to the next section.
- Mastering OpenLayers 3
- DBA攻堅(jiān)指南:左手Oracle,右手MySQL
- 高手是如何做產(chǎn)品設(shè)計(jì)的(全2冊(cè))
- Learning ROS for Robotics Programming(Second Edition)
- 深度學(xué)習(xí)經(jīng)典案例解析:基于MATLAB
- C語(yǔ)言程序設(shè)計(jì)基礎(chǔ)與實(shí)驗(yàn)指導(dǎo)
- Learning Linux Binary Analysis
- Go語(yǔ)言精進(jìn)之路:從新手到高手的編程思想、方法和技巧(1)
- 零基礎(chǔ)Java學(xué)習(xí)筆記
- Building Dynamics CRM 2015 Dashboards with Power BI
- Mockito Essentials
- HTML5+CSS3+JavaScript 從入門到項(xiàng)目實(shí)踐(超值版)
- 深度實(shí)踐KVM:核心技術(shù)、管理運(yùn)維、性能優(yōu)化與項(xiàng)目實(shí)施
- PHP項(xiàng)目開(kāi)發(fā)全程實(shí)錄(第4版)
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)