0

rspecでgemを使用して、アプリでgemのコントローラーをテストする方法を知っている人はいますか? http://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/http://say26.com/rspec-testing-controllers-を試しました成功しないレールアプリケーションの外側。

次のような宝石にコントローラーがあります。

module mygem
 class PostsController < ::ApplicationController
  def index
    @posts = Posts.find(:all)
    @other_var = 10        
  end
 end
end

そして、spec/controllers/posts_controller_spec.rbのようなアプリでテストしたいと思います

describe PostsController do
  describe "index" do

    it "has posts" do
      get :index
      assigns(:posts).should_not be_nil
    end

    it "has other var" do
      get :index
      assert_equal(10, assigns(:other_var))
    end

  end
end

そして私のspec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)    
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|

end

rspec は実際にはこれを意図したものではないことを知っています。アイデアや代替案も役立ちます。

4

2 に答える 2

0

宝石は宝石、アプリはアプリ。それらは別のものです。

gem のテストをアプリに混ぜるのは良い習慣ではないと思います。

通常、gem は十分にテストされているため、テストする必要はありません。本当にやりたい場合、または gem にテストが不足している場合は、gem をフォークしてローカルにプルし、そのテスト ファイルを開いて自分のファイルを追加します。その後、それを元に戻してこの gem を改善するか、独自のバージョンの gem にすることができます。

独自の gem を作成している場合は、テストを app ではなく gem に入れます。

アプリに追加された gem の機能をテストしたい場合は、統合された効果をテストできますが、単体テストは必要ありません。

于 2013-06-21T15:07:21.707 に答える
0

オーケー、今はばかげているように感じます。コントローラーに gem 名前空間を追加する必要がありました。それで

describe PostsController do
...
end

になる

describe mygem::PostsController do
...
end
于 2013-06-21T20:56:46.893 に答える