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 は実際にはこれを意図したものではないことを知っています。アイデアや代替案も役立ちます。