Rspec 让范围界定 [英] Rspec let scoping

查看:45
本文介绍了Rspec 让范围界定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我在 rspec let 和范围界定方面有问题.我可以在示例中使用 let 定义的方法(it"块),但不能在外部(我执行 let 的 describe 块)中使用.

I believe I have a problem with rspec let and scoping. I can use the methods defined with let in examples (the "it" blocks), but not outside (the describe block where I did the let).

5   describe Connection do
8     let(:connection) { described_class.new(connection_settings) }
9 
10    it_behaves_like "any connection", connection
24  end

当我尝试运行此规范时,出现错误:

When I try to run this spec, I get the error:

connection_spec.rb:10: 未定义的本地变量或方法连接"类:0xae8e5b8(NameError)

connection_spec.rb:10: undefined local variable or method `connection' for Class:0xae8e5b8 (NameError)

如何将连接参数传递给 it_behaves_like?

How can I pass the connection parameter to the it_behaves_like?

推荐答案

我找到了适合我的方法:

I found what works for me:

   describe Connection do
     it_behaves_like "any connection", new.connection
     # new.connection: because we're in the class context 
     # and let creates method in the instance context, 
     # instantiate a instance of whatever we're in
   end

这篇关于Rspec 让范围界定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆