在 RSpec 中,在 :all 块之前使用 let 变量 [英] In RSpec, using let variable inside before :all block
问题描述
我的大部分测试中都有以下代码:
描述'索引'let(:company) { FactoryGirl.create(:company) }let(:user) { FactoryGirl.create(:user, company: company) }在做之前登录用户访问 products_path结尾...结尾
但我收到以下警告:
警告:让声明 'user' 在 'before(:all)' 中访问
我的问题是,这样做的正确方法是什么?我找不到关于警告本身的太多信息.
谢谢!
我的目标是使用用户变量,以便我可以将它传递给 sign_in,它使用户登录,然后在另一个测试中使用它(我检查了用户)
我遇到了同样的问题,我通过在 before
块中将所有变量声明为属性来解决它:
现在你可以在你的测试中使用 @user
和 @company
,你不应该有任何警告.
I have the following code inside most of my tests:
describe 'index'
let(:company) { FactoryGirl.create(:company) }
let(:user) { FactoryGirl.create(:user, company: company) }
before do
sign_in user
visit products_path
end
...
end
But I'm getting the following warning:
WARNING: let declaration 'user' accessed in a 'before(:all)'
My question is, what is the correct way of doing this? I can't find much information about the warning itself.
Thanks!
EDIT: My goal is to use the user variable so I can pass it on to sign_in, which signs the user in, and use it later on another tests (I check for the company attribute of the User)
I had the same problem, I have solved it by declaring all my variables as attributes inside the before
block:
describe 'index' before(:all) do @company = FactoryGirl.create(:company) @user = FactoryGirl.create(:user, company: @company) sign_in @user visit products_path end ... end
Now you can use @user
and @company
inside your tests, and you shouldn't have any warnings.
这篇关于在 RSpec 中,在 :all 块之前使用 let 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!