原帖:http://elite28.github.com/2010/09/05/rspec%E6%A0%B7%E4%BE%8B.html
下面是一个ruby代码使用rspec进行行为驱动开发的样例:
mkdir rspec_tutorial cd rspec_tutorial mate .
新建一个测试驱动文件
touch user_spec.rb
编辑内容:
describe User do end
这段测试代码的意思是,描述用户行为 在终端运行
spec user_spec.rb
这样会发生一个错误
./user_spec.rb:1: uninitialized constant User (NameError)
这个错误提示的意思是说没有初始化user对象,原因是我们还没有User类,所以现在要创建一个User类: 新建一个user.rb文件,内容:
class User end
并且得包含到测试文件中(user_spec.rb)
require 'user' describe User do end
这次我们运行命令:
$ spec user_spec.rb . Finished in 0.010015 seconds 0 examples, 0 failures
这个输出告诉我们,现在还没有样例,所以我们可以这样在测试文件中定义:(user_spec.rb)
describe User do it "should be in any roles assigned to it" do end end
it方法会返回一个example实例,因此it可以理解成“用户行为的一个样例” 这次我们在执行spec命令的时候加-f 参数,让结果按照某种格式来输出:
spec user_spec.rb -f specdoc User - should be in any roles assigned to it Finished in 0.022585 seconds 1 example, 0 failures
现在已经有了example了,然后是添加行为(user_spec.rb)
require 'user' describe User do it "should be in any roles assigned to it" do user.should be_in_role("assigned role") end end
加入的这行代码的意思是user应该能够胜任所有分配给他的角色。运行测试: $ spec user_spec.rb -f specdoc
User - should be in any roles assigned to it (FAILED - 1) 1) NameError in 'User should be in any roles assigned to it' undefined local variable or method `user' for #<Spec::Example::ExampleGroup::Subclass_1:0x101547c28> ./user_spec.rb:4: Finished in 0.008971 seconds 1 example, 1 failure
又出错了,是的,但在继续之前,让我们先仔细看看这段出错信息:
还有一点需要注意:这段信息没有给出RSpec代码的backtrace,如果你需要它,可以通过–b选项来获取。
下面我们得在user_spec.rb中新建一个User对象:
require 'user' describe User do it "should be in any roles assigned to it" do user = User.new user.should be_in_role("assigned role") end end
再次执行测试: $ spec user_spec.rb -f specdoc
User - should be in any roles assigned to it (FAILED - 1) 1) NoMethodError in 'User should be in any roles assigned to it' undefined method `in_role?' for #<User:0x101547778> ./user_spec.rb:5: Finished in 0.008906 seconds 1 example, 1 failure
这次是因为User对象没有role_in?方法,修改user.rb:
class User def in_role?(role) end end
再次执行测试: $ spec user_spec.rb -f specdoc
User - should be in any roles assigned to it (FAILED - 1) 1) 'User should be in any roles assigned to it' FAILED expected in_role?("assigned role") to return true, got nil ./user_spec.rb:5: Finished in 0.008822 seconds 1 example, 1 failure
这段代码意思是调用in_role?方法返回nil,而不是true.所以通过测试很简单,直接在方法里面返回true即可:
class User def in_role?(role) true end end
执行测试:
$ spec user_spec.rb -f specdoc User - should be in any roles assigned to it Finished in 0.008493 seconds 1 example, 0 failures
但是到目前为止,我们在测试代码中还没分配给user对象角色,就对他进行了判断,所以应该在测试中加入(user_spec.rb):
require 'user' describe User do it "should be in any roles assigned to it" do user = User.new user.assign_role("assigned role") user.should be_in_role("assigned role") end end
再次执行测试代码不出意外会有下面的提示:
1) NoMethodError in 'User should be in any roles assigned to it' undefined method `assign_role' for #<User:0x101547408> ./user_spec.rb:5:
user.rb中没有assign_role的方法,所以添加一个(user.rb):
class User def assign_role(role) end def in_role?(role) true end end
这次再执行测试,就可以通过了。 但是,现在,我们只是解决了“用户必须接受所有分配给他的角色”,但是还有一个问题就是”用户不应该接受没有分配给他的角色“。所以我们需要为用户行为再增加一个样例:
require 'user' describe User do it "should be in any roles assigned to it" do user = User.new user.assign_role("assigned role") user.should be_in_role("assigned role") end it "不该属于没有分配给他的角色" do user = User.new user.should_not be_in_role("unassigned role") end end
执行测试:
$ spec user_spec.rb -f specdoc User - should be in any roles assigned to it - 不该属于没有分配给他的角色 (FAILED - 1) 1) 'User 不该属于没有分配给他的角色' FAILED expected in_role?("unassigned role") to return false, got true ./user_spec.rb:11: Finished in 0.009124 seconds 2 examples, 1 failure
失败,用户接受了没有分配给他的角色,这时,我们需要对User.rb代码进行改动:
class User def assign_role(role) end def in_role?(role) role == "assigned role" end end
再次执行代码,没错了。 但是user.rb中有些重复,都是用了assigned role,因此要对user.rb的代码进行重构:
class User def assign_role(role) @role = role end def in_role?(role) role == @role end end
再来测试一下:
$ spec user_spec.rb -f specdoc User - should be in any roles assigned to it - 不该属于没有分配给他的角色 Finished in 0.008864 seconds 2 examples, 0 failures