【问题标题】:Access the current user in Cucumber features - Devise在 Cucumber 功能中访问当前用户 - 设计
【发布时间】:2011-06-10 12:56:35
【问题描述】:

注意:我对 Cucumber 很陌生。

我正在尝试做一个通用步骤(不确定某个地方是否已经存在),以便在存在关联的情况下,您可以轻松地将对象添加到另一个对象。我想做类似的事情:

manage_notes.feature

Background: Login User
  Given the following user records
    | email          | password |
    | test@email.com | password |
  Given I am logged in as "test@email.com" with password "password"

Scenario: Edit existing note
  Given I have already created a note that belongs to current_user

general_steps.rb

Given /^the following (.+) records?$/ do |factory, table|
  table.hashes.each do |hash|
    Factory(factory, hash)
  end
end

Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |email, password|
  unless email.blank?
    visit new_user_session_path
    fill_in "Email", :with => email
    fill_in "Password", :with => password
    click_button "Sign In"
  end
end

note_steps.rb

  Given /^I have already created a (.+) that belongs to (.+)$/ do |factory, resource|
      model = Factory(factory)
      resource.send(model.class.to_s.downcase.pluralize) << model
  end

似乎有一种方法可以使用 devise 'current_user' helper。

访问已登录用户的正确方法是什么?

如果您需要更多信息,请告诉我。 谢谢!

更新 1: 我通过创建一个允许我执行以下操作的新步骤暂时解决了我的问题:

Given I have already created a note that is owned by the user with email "test@email.com"

但我不想指定电子邮件,如果可能的话,我仍然希望能够使用已登录的用户。

更新 2: 添加了general_steps.rb

所以你可以看到,在我的“后台”中,用户是通过工厂创建的,然后通过我的界面登录。我想访问该登录用户的模型。

【问题讨论】:

    标签: ruby-on-rails devise cucumber integration-testing


    【解决方案1】:

    我不使用 Devise,所以我无法具体回答 Devise 是否有访问 current_user 的方法。

    但我实际上喜欢使用Pickle 来帮助我保留我的推荐信。也许这可以帮助你,直到你找到一种更具体的方式来实现你想要的。

    Given /^the following (.+) records$/ do |factory, table|
      table.hashes.each do |hash|
        Factory(factory, hash)
    
        # since this is like an all encompassing factory creator, this line to 
        # create a Pickle references might need a bit more extra work if you want
        # to create references for other factory types
    
        # I assume email is unique, else use a unique identifier of some sort
        find_model! %{user: "#{hash['email']}"}, {:email => hash['email']} 
      end
    end
    
    Given /^I have already created a (.+) that belongs to #{capture_model}$/ do |factory, name|
      model = Factory(factory)
      ref = model!(name) # we find that reference here
      ref.send(model.class.to_s.downcase.pluralize) << model
    end
    

    这样写

    Given I have already created a note that belongs to user: "test@email.com"
    
    # I would just change this to 
    Given I have already created a note
    
    # or
    Given a note was created for user: "test@email.com"
    

    你是I,既然你说Given I logged in...,就不用说that belongs to user: "test@email.com",已经是you了。

    更不用说当您阅读它时可能会导致混淆,有些人可能会认为您正在向用户添加注释,他们现在可能知道(或意识到)实际上是您自己。


    虽然您仍然必须明确引用(例如用户:“John Doe”),但我认为这是一个优点。通过始终调用特定的引用,每个人都知道被引用的是谁,并且毫无疑问谁在做什么。

    Pickle 很好地为我们服务。我们发现唯一有问题的地方是直接通过应用程序的 ui 创建的东西,这有点棘手,以确保您创建对它的正确引用。

    泡菜有很多用途,所以一定要看看。


    更新

    你必须在这里找到自己。因为,如您所愿,没有current_user 选项(据我们所知)。所以你基本上必须去找这个场景中的“演员”。

    Given /^I have already created a note$/ do
      user = model!(%{user: "test@email.com"}) # if using pickle
      # user = User.find_by_email("test@email.com") # if just regular AR
      user.notes << Note.create
    end
    

    【讨论】:

    • 谢谢,感谢您为此付出的时间。我已将其标记为有用。我会在更方便的时候更详细地检查它,并确定它是否适合我。
    • 好吧,你是绝对正确的,我应该删除“属于用户:”部分。我如何访问“我”?我想做'我已经创建了一个笔记'。在我的步骤中,我可以使用“假设存在带有内容的注释:“你好”,但是我如何将 user_id 分配为登录用户?
    • 我想我很难在我的步骤中弄清楚“我”是谁。我可以使用密码“密码”执行“我以“电子邮件”身份登录。但是当我想做'我已经创建了一个笔记'时,我如何获得登录用户的参考?
    • 我更新了答案,给你一个如何找到自己的例子(或者至少我会如何找到自己)。你必须通过你的测试来管理那个“我”是谁。要么通过像pickle 这样的宝石,要么通过其他方式。抱歉回复晚了,我得把培根带回家。
    • 我明白你的意思了,哈哈,晚回复没问题。将电子邮件硬编码到自定义步骤中似乎不是一个好主意。如果我必须在功能的“背景”中更改电子邮件,那么我必须在多个地方进行更改。你明白吗?
    【解决方案2】:

    我只是很简单地解决它: 我已经定义了“FactoryGirl”用户,然后这个方法......

    Given(/^I am users logged in as "(.*?)" with password "(.*?)"$/) do |email, pass|
      visit new_user_session_path
      fill_in "user_email", :with => email
      fill_in "user_password", :with => pass
      click_button I18n.t("login.sign_in")
      @current_user = User.find_by_email(email)
    end
    

    进一步说明您可以在您的步骤中使用@current_user

    【讨论】:

      【解决方案3】:

      这个话题很老了,但这是我使用的解决方案。例如,您必须在 env.rb 中加载此模块。使用它,您可以在步骤中使用 @current_user 或 current_user 访问当前用户。

      module UserSessionHelper
        def current_user
          @current_user
        end
      
        def login(user=nil)
          if user.nil?
            @current_user = FactoryGirl.create(:user, username: fake_name)
          else
            @current_user = user
          end
          visit login_path
          fill_in 'Username', with: @current_user.username
          fill_in 'Password', with: '1234'
          click_button 'Login'
          page.should have_content('Logout')
        end
      
        def logout
          click_link 'Logout'
        end
      end
      
      RSpec.configure do |config|
        config.include UserSessionHelper
      end if RSpec.respond_to?(:configure)
      
      World(UserSessionHelper) if respond_to?(:World)
      

      【讨论】:

        【解决方案4】:

        您应该能够为此使用上下文: http://cuke4ninja.com/sec_sharing_context.html

        有一个步骤,您以用户身份登录,将其存储在所有步骤都可以访问的共享上下文中,然后在以下步骤中访问它。

        希望这会有所帮助,并且我没有误解问题。

        祝你好运!

        【讨论】:

        • 根据您的描述,这听起来像是在每个场景之前运行的东西,对吧?这不正是我想要的。我已经拥有在每个步骤之前运行的背景,在该背景下,用户登录。我只是不确定如何“访问”当前登录的用户的用户模型。在设计中我可以调用“当前用户”。
        • 我不确定这就是我想要的,除非我误解了。
        • 它在java中的工作方式(我只使用了java实现)是你定义一个将在每一步中注入的上下文。因此,在第一个登录步骤中,您执行登录步骤并设置变量 current_user,然后从可用于其他每个步骤的上下文对象中引用变量 current_user。这有帮助吗?
        • 我明天在工作时会再看看这个。我觉得有一个更简单的方法。你以前用过 Rails/Devise 吗?
        • 该链接现在转到一个提供约会建议的网站:(
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-10
        • 2017-10-14
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多