【问题标题】:calling helper method inside rspec在 rspec 中调用辅助方法
【发布时间】:2013-06-02 03:40:28
【问题描述】:

我有一个帮助程序检查用户是否登录:

sessions_helper.rb:

module SessionsHelper
  def signed_in?
    !current_user.nil? # if current user is not nil then user is signed in
  end
end

我想在使用 rspec 执行某些操作后测试用户是否已登录:

my_spec.rb:

require 'spec_helper'
describe "after clicking button" do
    before  {click_button someButton}
    it "should be signed in" do
      signed_in? != nil
    end
end

那么我该如何让我的登录?我的规范可以访问的方法?我只需要添加: require 'sessions_helper'?我是否最好在我的 spec_helper.rb 文件中添加一些内容,以便我的所有 spec.rb 文件都可以访问此方法?

【问题讨论】:

  • 别忘了给should打电话,否则你的测试是没用的(但是是绿色的)。

标签: ruby-on-rails rspec


【解决方案1】:
require 'spec_helper'

describe SessionsHelper
  describe "after clicking button" do
    before  {click_button someButton}
    it "should be signed in" do
      helper.signed_in? != nil
    end
  end
end

您可以在对象“helper”上调用辅助方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2015-11-12
    • 2012-05-19
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多