 
- 厨师教程
- 厨师 - 主页
- 厨师 - 概述
- 厨师 - 建筑
- Chef - 版本控制系统设置
- 厨师 - 工作站设置
- Chef - 客户端设置
- 厨师 - 测试厨房设置
- 厨师 - 刀具设置
- 厨师 - 独奏设置
- 厨师 - 食谱
- Chef - 食谱依赖项
- 厨师 - 角色
- 厨师 - 环境
- Chef - Chef-Client 作为守护进程
- 厨师 - Chef-Shell
- 厨师 - 测试食谱
- 厨师 - 美食评论家
- 厨师 - ChefSpec
- 使用测试厨房测试食谱
- 厨师 - 节点
- 厨师 - 厨师-客户运行
- 高级厨师
- 动态配置菜谱
- 厨师 - 模板
- Chef - 带有 Chef DSL 的纯 Ruby
- 厨师 - 红宝石宝石与食谱
- 厨师 - 图书馆
- 厨师 - 定义
- Chef - 环境变量
- 厨师 - 数据袋
- Chef - 数据包脚本
- Chef - 跨平台食谱
- 厨师 - 资源
- 轻量级资源提供者
- 厨师 - 蓝图
- Chef - 文件和包
- 厨师 - 社区食谱
- 厨师有用的资源
- 厨师 - 快速指南
- 厨师 - 有用的资源
- 厨师 - 讨论
厨师 - ChefSpec
测试驱动开发(TDD)是一种在编写任何实际配方代码之前编写单元测试的方法。测试应该是真实的并且应该验证食谱的作用。它实际上应该失败,因为没有开发出配方。一旦配方开发出来,测试就应该通过。
ChefSpec 基于流行的 RSpec 框架构建,并提供用于测试 Chef 配方的定制语法。
创建 ChefSpec
步骤 1 - 创建一个包含 ChefSpec gem 的 gem 文件。
vipin@laptop:~/chef-repo $ subl Gemfile source 'https://rubygems.org' gem 'chefspec'
步骤 2 - 安装 gem。
vipin@laptop:~/chef-repo $ bundler install Fetching gem metadata from https://rubygems.org/ ...TRUNCATED OUTPUT... Installing chefspec (1.3.1) Using bundler (1.3.5) Your bundle is complete!
步骤 3 - 创建一个spec目录。
vipin@laptop:~/chef-repo $ mkdir cookbooks/<Cookbook Name>/spec
第 4 步- 创建规范
vipin@laptop:~/chef-repo $ subl  
cookbooks/my_cookbook/spec/default_spec.rb  
require 'chefspec'  
describe 'my_cookbook::default' do  
   let(:chef_run) {  
      ChefSpec::ChefRunner.new(  
         platform:'ubuntu', version:'12.04'  
      ).converge(described_recipe)  
   }  
   it 'creates a greetings file, containing the platform  
   name' do  
      expect(chef_run).to  
      create_file_with_content('/tmp/greeting.txt','Hello! ubuntu!')  
   end  
end 
步骤 5 - 验证 ChefSpec。
vipin@laptop:~/chef-repo $ rspec cookbooks/<Cookbook Name>/spec/default_spec.rb 
F 
Failures: 
1) <CookBook Name> ::default creates a greetings file, containing the platform name 
Failure/Error: expect(chef_run.converge(described_recipe)).to 
create_file_with_content('/tmp/greeting.txt','Hello! ubuntu!') 
File content: 
does not match expected: 
Hello! ubuntu! 
# ./cookbooks/my_cookbook/spec/default_spec.rb:11:in `block 
(2 levels) in <top (required)>' 
Finished in 0.11152 seconds 
1 example, 1 failure  
Failed examples: 
rspec ./cookbooks/my_cookbook/spec/default_spec.rb:10 # my_ 
cookbook::default creates a greetings file, containing the 
platform name 
步骤 6 - 编辑食谱默认食谱。
vipin@laptop:~/chef-repo $ subl cookbooks/<Cookbook Name>/recipes/default.rb template '/tmp/greeting.txt' do variables greeting: 'Hello!' end
步骤 7 - 创建模板文件。
vipin@laptop:~/chef-repo $ subl cookbooks/< Cookbook Name>/recipes/default.rb <%= @greeting %> <%= node['platform'] %>!
步骤 8 - 再次运行 rspec。
vipin@laptop:~/chef-repo $ rspec cookbooks/<Cookbook Name>/spec/default_spec.rb . Finished in 0.10142 seconds 1 example, 0 failures
怎么运行的
为了使其正常工作,我们需要首先设置基础设施,以便将 RSpec 与 Chef 结合使用。然后我们需要 ChefSpec Ruby gem,并且说明书需要一个名为 spec 的目录,其中将保存所有测试。