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

访问量: 575

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

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

我的 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 

订阅/RSS Feed

Subscribe

分类/category