Cucumber 中的 World

本文介绍RSpec与Cucumber结合使用的方法,通过RSpec stubs增强Cucumber场景测试能力。每场测试在一个干净的World对象中运行,确保独立性和可重复性。支持Rails项目的集成测试,并可以通过扩展模块来自定义World对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[quote]require 'spec/stubs/cucumber'[/quote]
The spec/stubs/cucumber ships with RSpec (as of version 1.2.8), and
adds behaviour to the Cucumber [b]World [/b]to support using RSpec stubs
in Cucumber scenarios.3

To ensure that each cucumber scenario starts with a clean slate, your scenarios are run upon a blank Object.new object. Or in a Rails project its a new Rails test session ActionController::IntegrationTest.

These are called World objects (see cucumber wiki). You pass in a specific World object you’d like to use, else it defaults to Object.new For a Rails project, you’re using Cucumber::Rails::World.new for your world object each time, which is a subclass of ActionController::IntegrationTest.

The benefit of a World object starting point for each scenario is that you can add methods to it, that won’t affect the rest of the Ruby world you live in: which will be the Cucumber runner. That is, you cannot accidently blow up Cucumber.


World(*world_modules, &proc)


Registers any number of world_modules (Ruby Modules) and/or a Proc. The proc will be executed once before each scenario to create an Object that the scenario’s steps will run within. Any world_modules will be mixed into this Object (via Object#extend).

This method is typically called from one or more Ruby scripts under features/support. You can call this method as many times as you like (to register more modules), but if you try to register more than one Proc you will get an error.

Cucumber will not yield anything to the proc. Examples:

   World do
MyClass.new
end

World(MyModule)


[b]########been excuted#########
########been excuted#########
########been excuted#########
You can only pass a proc to #World once, but it's happening
in 2 places:

/world.rb:27:in `World'
vendor/plugins/email-spec/features/support/env.rb:14:in `World'[/b]




# File 'lib/cucumber/rb_support/rb_dsl.rb', line 47

def World(*world_modules, &proc)
RbDsl.build_rb_world_factory(world_modules, proc)
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值