如何在 Rails 中测试助手?

发布于 2024-07-11 09:30:13 字数 60 浏览 8 评论 0原文

我正在尝试构建一些单元测试来测试我的 Rails 助手,但我永远不记得如何访问它们。 恼人的。 建议?

I'm trying to build some unit tests for testing my Rails helpers, but I can never remember how to access them. Annoying. Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别理我 2024-07-18 09:30:13

在 Rails 3 中你可以这样做(事实上这就是生成器创建的):

require 'test_helper'

class YourHelperTest < ActionView::TestCase
  test "should work" do
    assert_equal "result", your_helper_method
  end
end

当然还有 Matt Darby 的 rspec 变体 也适用于 Rails 3

In rails 3 you can do this (and in fact it's what the generator creates):

require 'test_helper'

class YourHelperTest < ActionView::TestCase
  test "should work" do
    assert_equal "result", your_helper_method
  end
end

And of course the rspec variant by Matt Darby works in rails 3 too

咋地 2024-07-18 09:30:13

您可以在 RSpec 中执行相同的操作:

require File.dirname(__FILE__) + '/../spec_helper'

describe FoosHelper do

  it "should do something" do
    helper.some_helper_method.should == @something
  end

end

You can do the same in RSpec as:

require File.dirname(__FILE__) + '/../spec_helper'

describe FoosHelper do

  it "should do something" do
    helper.some_helper_method.should == @something
  end

end
半夏半凉 2024-07-18 09:30:13

从这里窃取: http://joakimandersson.se/archives/ 2006/10/05/test-your-rails-helpers/

require File.dirname(__FILE__) + ‘/../test_helper’
require ‘user_helper’

class UserHelperTest < Test::Unit::TestCase

include UserHelper

def test_a_user_helper_method_here
end

end

[从 Matt Darby 窃取,他也在该线程中写道。] 您可以在 RSpec 中执行相同的操作:

require File.dirname(__FILE__) + '/../spec_helper'

describe FoosHelper do

  it "should do something" do
    helper.some_helper_method.should == @something
  end

end

Stolen from here: http://joakimandersson.se/archives/2006/10/05/test-your-rails-helpers/

require File.dirname(__FILE__) + ‘/../test_helper’
require ‘user_helper’

class UserHelperTest < Test::Unit::TestCase

include UserHelper

def test_a_user_helper_method_here
end

end

[Stolen from Matt Darby, who also wrote in this thread.] You can do the same in RSpec as:

require File.dirname(__FILE__) + '/../spec_helper'

describe FoosHelper do

  it "should do something" do
    helper.some_helper_method.should == @something
  end

end
伪心 2024-07-18 09:30:13

该线程有点旧,但我想我会用我使用的内容来回复:

# encoding: UTF-8

require 'spec_helper'

describe AuthHelper do

  include AuthHelper # has methods #login and #logout that modify the session

  describe "#login & #logout" do
    it "logs in & out a user" do
      user = User.new :username => "AnnOnymous"

      login user
      expect(session[:user]).to eq(user)

      logout
      expect(session[:user]).to be_nil
    end
  end

end

This thread is kind of old, but I thought I'd reply with what I use:

# encoding: UTF-8

require 'spec_helper'

describe AuthHelper do

  include AuthHelper # has methods #login and #logout that modify the session

  describe "#login & #logout" do
    it "logs in & out a user" do
      user = User.new :username => "AnnOnymous"

      login user
      expect(session[:user]).to eq(user)

      logout
      expect(session[:user]).to be_nil
    end
  end

end
赢得她心 2024-07-18 09:30:13

我刚刚在另一个线程上发布了这个答案,询问了同样的问题。 我在我的项目中做了以下事情。

require_relative '../../app/helpers/import_helper'

I just posted this answer on another thread asking the same question. I did the following in my project.

require_relative '../../app/helpers/import_helper'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文