非常奇怪的rails3 rspec (describe) 问题: 慎重使用嵌套describe

非常奇怪的问题。 也许是自己没有深入看过rspec 文档。 [color=darkblue]慎重使用 嵌套的describe[/color]

今天使用了 gem : state_machine ([url=https://github.com/pluginaweek/state_machine] https://github.com/pluginaweek/state_machine[/url])

我的 Gemfile

gem 'rails', '3.2.0'
gem "state_machine", "1.1.2"
group :development, :test do
gem 'rspec', '2.8.0'
gem 'rspec-rails', '2.8.1'
end



在投入开发之前,我写了 单元测试 和对应的实现代码:

spec/models/contract_spec.rb

require 'spec_helper'
describe Contract do
# 之前的很多测试用例
describe "state machine" do
it "should transit its state to ready_to_review" do
contract = Contract.new
contract.ready_to_review
contract.state.should == Contract::STATE_READY_TO_REVIEW.to_s
end
end
end


app/models/contract.rb :

class Contract
STATE_CREATED= :created
STATE_READY_TO_REVIEW =:ready_to_review
state_machine :state, :initial => STATE_CREATED do
event :ready_to_review do
transition STATE_CREATED => STATE_READY_TO_REVIEW
end
end
end


估计各种修改,DEBUG之下跑了50遍, contract_spec.rb 还是通不过。 state 总是不对。

但是下面的却可以通过(脱离了rails 环境,直接 ruby state_machine.rb )
require 'rubygems'
require 'state_machine'
require 'test/unit'
class Contract
STATE_CREATED= :created
STATE_READY_TO_REVIEW =:ready_to_review
state_machine :state, :initial => STATE_CREATED do
event :ready_to_review do
transition STATE_CREATED => STATE_READY_TO_REVIEW
end
end
end

class ContractTest < Test::Unit::TestCase
def test_should_transition
contract = Contract.new
puts "contract.state: #{contract.state}"
assert contract.state == Contract::STATE_CREATED.to_s
assert contract.can_ready_to_review? == true
result =contract.ready_to_review
puts "result: #{result}"
puts "contract.state: #{contract.state}"
assert contract.state == Contract::STATE_READY_TO_REVIEW.to_s
end
end


后来无语了,休息了一下,突然想到,是不是describe 的问题。 于是把

describe "state machine" do
这一行注释掉。 居然就好了! 我无语啊!


require 'spec_helper'
describe Contract do
# 注释掉了外层的 describe
#describe "state machine" do
it "should transit its state to ready_to_review" do
contract = Contract.new
contract.ready_to_review
contract.state.should == Contract::STATE_READY_TO_REVIEW.to_s
end
#end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值