如何在functionrspec中添加上下文?

我想在我的functionrspec中添加上下文,但我不确定上下文和场景之间有什么意义。

这是我到目前为止:

feature 'As an admin I manage the orders of the system' do context 'Logged in user who is an admin' do before(:each) do admin = create(:admin_user) login_as(admin, :scope => :user) end scenario 'User sees all the orders' do visit admin_orders_path expect(page).to have_content('List of orders') end end context 'Logged in user who is not an admin' do before(:each) do user = create(:user) login_as(user, :scope => :user) end scenario 'User cannot see the orders' do visit admin_orders_path expect(current_path).to eq('/') end end end 

这是否有意义,或者我应该在使用场景或上下文之间做出决定,但不是两者都在一起?

我在考虑测试的方式如下:

Feature规范是用于测试整个“图片”的高级测试 – 应用程序的function。

所以,我的应用程序的核心function:内部scenarios features

unit testing: describeit

您可以在两者中使用context

从文档中 : featurescenario对应于describeit 。 这些方法只是一些别名,允许function规范更多地作为客户测试和验收测试。

所以,在你的情况下,我会做以下事情:

 feature 'As an admin I manage the orders of the system' do context 'user is logged in as' do before(:each) do user = create(:user) login_as(user, :scope => :user) end scenario 'an admin, can see all the orders' do visit admin_orders_path expect(page).to have_content('List of orders') end scenario 'not an admin, cannot see the orders' do visit admin_orders_path expect(current_path).to eq('/') end end 

一个context ,用户的两个scenarios 。 另外,我会更多地考虑一下该function的描述。 希望有所帮助,不要让你更加困惑!

此外,我喜欢看我的测试,如我的应用程序的“心跳”。 首先,您从function测试(外部,核心function)开始,然后进入内部(unit testing)。 那件事情一直在重复,就像“心跳”一样

根据文件 :

特征和场景DSL分别对应于describeit 。 这些方法只是一些别名,允许function规范更多地作为客户测试和验收测试。

在编写特征规范时,您可以简单地用feature替换describe (或context )。 feature语句应该在嵌套时起作用,如describe