有谁知道如何使用带有 rspec 的 gem 在应用程序中测试 gem 的控制器?我试过http://codingdaily.wordpress.com/2011/01/14/test-a-gem-with-the-rails-3-stack/和http://say26.com/rspec-testing-controllers- outside-of-a-rails-application没有成功。
我在这样的 gem 中有一个控制器:
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 并不是真的为此,想法或替代方案也会有所帮助。