RSpec Tutorial
RSpec Resources
Selected Reading
选读
- RSpec - Expectations
- RSpec - Filtering
- RSpec - Metadata
- RSpec - Helpers
- RSpec - Subjects
- RSpec - Tags
- RSpec - Hooks
- RSpec - Stubs
- RSpec - Test Doubles
- RSpec - Matchers
- RSpec - Writing Specs
- RSpec - Basic Syntax
- RSpec - Introduction
- RSpec - Home
RSpec Resources
Selected Reading
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
选读
RSpec - Hooks
RSpec - Hooks
当你书写单位测试时,在检测前后,往往易于操作固定和冲破法。 规定法是将测试条件混为一谈或“设定”的守则。 排污法确实是清理,它确保环境在随后的测试中处于一致状态。
一般而言,你的测试应相互独立。 当你进行一整套测试并且其中一次测试失败时,你希望相信,测试失败是因为该守则有点 b,而不是因为以前的测试使环境处于一个不一致的国家。
流.之前和之后,在RSpec使用的最常见hoo。 它们为确定和操作上文讨论的编织和冲破法提供了途径。 让我们考虑这一范例守则——
class SimpleClass attr_accessor :message def initiapze() puts " Creating a new instance of the SimpleClass class" @message = howdy end def update_message(new_message) @message = new_message end end describe SimpleClass do before(:each) do @simple_class = SimpleClass.new end it should have an initial message do expect(@simple_class).to_not be_nil @simple_class.message = Something else. . . end it should be able to change its message do @simple_class.update_message( a new message ) expect(@simple_class.message).to_not be howdy end end
当你管理该守则时,你就获得了以下产出:
Creating a new instance of the SimpleClass class . Creating a new instance of the SimpleClass class . Finished in 0.003 seconds (files took 0.11401 seconds to load) 2 examples, 0 failures
让我们更仔细地审视一下所发生的情况。 以前(每个)的方法是我们确定制定法典的方法。 当你通过以下论点时,你正在指示前一种方法,在你的实例小组中,即上面法典中描述的栏目中的两条。
在这一行文中:@simple_class = 简单的地图集。 你们会想到什么目标? RSpec在描述区块的幕后设置了一个特殊类别。 这使你能够把价值划分为这一类变量,使你能够在其实例中查阅。 这还使我们容易在测试中撰写更清洁的法典。 如果每一次测试(例)都需要简单的地图集,我们就可以把该守则放在前面,而不必逐例加以补充。
通知说,“开创一个简单的地图集新例”两字已写给议会,这表明,在“it>的每个栏目中都打上了记号。
正如我们前面提到的那样,RSpec也有一个 after子,而之前和之后,都能够做到:所有这些都是论点。 之后的 h将低于规定的目标。 指标:所有指标都意味着,所有例子都将在前后进行。 这里是一个简单的例子,说明每一方何时被点击。
describe "Before and after hooks" do before(:each) do puts "Runs before each Example" end after(:each) do puts "Runs after each Example" end before(:all) do puts "Runs before all Examples" end after(:all) do puts "Runs after all Examples" end it is the first Example in this spec file do puts Running the first Example end it is the second Example in this spec file do puts Running the second Example end end
在你管理上述法典时,你将看到这一产出。
Runs before all Examples Runs before each Example Running the first Example Runs after each Example .Runs before each Example Running the second Example Runs after each Example .Runs after all ExamplesAdvertisements