【发布时间】:2013-06-21 14:57:23
【问题描述】:
有谁知道如何使用带有 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 并不是真的为此而生,想法或替代方案也会有所帮助。
【问题讨论】:
-
你试过this question的答案了吗?
-
我的应用中没有像这样的 gem 控制器的路径。
-
您是否考虑过让您的 gem 成为 Rails 引擎?听起来它会更合适,因为通常宝石不包括控制器。 edgeguides.rubyonrails.org/engines.html
标签: ruby-on-rails testing rspec