Ruby 什么是';让';在Rspec?
我尝试了以下方法:Ruby 什么是';让';在Rspec?,ruby,rspec,let,Ruby,Rspec,Let,我尝试了以下方法: describe "#check_recurring_and_send_message" do let(:schedule) {ScheduleKaya.new('test-client-id')} context "when it is 11AM and recurring event time is 10AM" do schedule.create_recurring_event('test-keyword', 'slack', 'd
describe "#check_recurring_and_send_message" do
let(:schedule) {ScheduleKaya.new('test-client-id')}
context "when it is 11AM and recurring event time is 10AM" do
schedule.create_recurring_event('test-keyword', 'slack', 'day', '10 AM')
it "sends an SMS" do
end
it "set the next_occurrence to be for 10AM tomorrow" do
tomorrow = Chronic.parse("tomorrow at 10AM")
expect(schedule.next_occurrence).to eq(tomorrow)
end
end
end
我在范围内发现了一个错误:
`method_missing': `schedule` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
不仅在这个例子中,而且在其他情况下,我不完全理解let和Rspec中创建实例所允许的范围是什么
在这里让let
与我用schedule=blah blah创建相比,有什么使用案例
我想我理解了这个错误的字面意思:我不能在上下文中使用计划
,只能在it中使用。
但是在这个示例中,如何正确地将内容放在描述、上下文、,或者它,以什么方式?让
被惰性地评估,当您想要在测试之间共享变量时,这很好,但只有当测试需要它时
从文档中:
使用let定义已记忆的辅助对象方法。该值将被缓存
跨同一示例中的多个调用,但不跨示例
请注意,let是惰性计算的:直到第一个
调用它定义的方法的时间。你可以用let!逼迫
方法在每个示例之前的调用
默认情况下,let是线程安全的,但您可以将其配置为非线程安全
禁用config.threadsafe,这会使let的执行速度加快一点
由于这一行,这里缺少一个方法:
schedule.create_recurring_event('test-keyword', 'slack', 'day', '10 AM')
似乎您希望在该上下文中的每个It
块之前对该行求值。你可以这样重写它:
describe "#check_recurring_and_send_message" do
let(:schedule) {ScheduleKaya.new('test-client-id')}
context "when it is 11AM and recurring event time is 10AM" do
before(:each) do
schedule.create_recurring_event('test-keyword', 'slack', 'day', '10 AM')
end
it "sends an SMS" do
end
it "set the next_occurrence to be for 10AM tomorrow" do
tomorrow = Chronic.parse("tomorrow at 10AM")
expect(schedule.next_occurrence).to eq(tomorrow)
end
end
end
谢谢我认为这是有道理的。所以我可以在上下文中使用before块?我不限于“it”块。只有在描述级别才应该使用let?在before块中不能使用let
。它位于示例之外的描述块或嵌套描述块中。before
块中的代码在与示例相同的上下文中运行。