本文介绍了如何在功能 rspec 中添加上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的特性 rspec 中添加上下文,但我不确定上下文和场景之间的意义.

I want to add context into my feature rspec, but I am not sure what makes sense between context and scenario.

这是我目前所拥有的:

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

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

Does this makes sense, or I should decide between using scenario or context, but not both together?

推荐答案

我的测试思路如下:

Feature 规范是用于测试整个图片" - 应用程序功能的高级测试.

Feature specs are high-level tests for testing the whole "picture" - functionality of your application.

所以,我的应用程序的核心功能:featuresscenarios 里面.

So, core functionality of my app: features with scenarios inside.

单元测试:describeit.

您可以在两者中使用 context.

You can use context in both though.

来自文档:featurescenario 分别对应于 describeit.这些方法只是别名,允许特性规范阅读更多内容作为客户测试和验收测试.

From the documentation:The feature and scenario correspond to describe and it, respectively. These methods are simply aliases that allow feature specs toread more as customer tests and acceptance tests.

因此,在您的情况下,我会执行以下操作:

So, in your case I would do the following:

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 给用户.另外,我会多考虑一下该功能的描述.希望对您有所帮助,不要让您更加困惑!

One context, two scenarios for the user. Also, I would think a little bit more about the description of the feature. Hope that helps and doens't confuse you more!

此外,我喜欢看到我的测试就像我的应用程序的心跳"一样.首先,您从功能测试(外部,核心功能)开始,然后进入内部(单元测试).而那件事一直在重复,就像心跳"

Also, I like to see my tests like the "heartbeat" of my app. First, you go from feature testing (outside, core functionality) and then you go inside(unit test). And that thing is repeating all the time, like a "heartbeat"

这篇关于如何在功能 rspec 中添加上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 09:24