Ruby on rails RSpec首先读取上下文,然后在(:all)之前读取。为什么?
我的环境:Ruby on rails RSpec首先读取上下文,然后在(:all)之前读取。为什么?,ruby-on-rails,ruby,debugging,rspec,Ruby On Rails,Ruby,Debugging,Rspec,我的环境: jruby-1.5.3 Rails 2.3.8 RSpec 1.3.1 Windows 7 (64-bit machine) 使用以下源代码运行Rspec,为什么Rspec读取(:each)之前语句之前的上下文中标有“=>”的行。非常感谢您的帮助 def save_env @host_os = Config::CONFIG['host_os'] end def restore_env Config::CONFIG['host_os'] = @host_os end de
jruby-1.5.3
Rails 2.3.8
RSpec 1.3.1
Windows 7 (64-bit machine)
使用以下源代码运行Rspec,为什么Rspec读取(:each)之前语句之前的上下文中标有“=>”的行。非常感谢您的帮助
def save_env
@host_os = Config::CONFIG['host_os']
end
def restore_env
Config::CONFIG['host_os'] = @host_os
end
describe Manager::ManagerConfig do
before(:each) do
save_env
end
after(:each) do
restore_env
end
context "Within Linus/Solaris environment" do
=> Config::CONFIG['host_os'] = 'linux'
it "should return the correct manager path under linux/solaris" do
# bar
end
it "should return the correct license path under windows env" do
# foo
end
end
end
def save_env
@host\u os=Config::Config['host\u os']
结束
def恢复_环境
Config::Config['host\u os']=@host\u os
结束
描述管理器::管理器配置do
在…之前做
拯救环境
结束
在(:每个)之后做
恢复环境
结束
上下文“在Linus/Solaris环境中”执行
=>Config::Config['host\u os']='linux'
它“应该在linux/solaris下返回正确的管理器路径”do
#酒吧
结束
它“应该在windows环境下返回正确的许可证路径”do
#福
结束
结束
结束
上下文设置了一个内部类,因此它内部的行将在加载时执行,除了每个it
、之前的和之后的创建一个稍后执行的代码块之外
您需要做的就是将配置设置包装在它自己的before(:each)
块中,顺序将是您期望的:外部before(:each)
,然后内部before(:each)
,然后it
:
before(:each) do
Config::CONFIG['host_os'] = 'linux'
end