如何控制器测试 rails 4 引擎重定向回 main_app 路由 [英] How to controller test rails 4 engine redirect back to main_app routes
问题描述
我在控制器测试我创建的 rails 引擎时遇到了一些问题.我遇到的问题是我似乎无法让 rspec 使用 main_app 路由.
I am having some trouble controller testing a rails engine that I created. The issue I am having is I can't seem to get rspec to use main_app routes.
例如,我正在编写以下控制器测试.如果不是 main_app.root_path 返回 nil,则测试将通过.
For instance, I am writing the following controller test. The test would pass if it wasn't for the main_app.root_path returning nil.
我的控制器规范中的代码是:
The code I have in my controller spec is:
context "successful session creation" do
let(:user) {FactoryGirl.create :user}
it 'sets user id session' do
get :create, use_route: :authenticatable, email: user.email, password: 'password'
expect(session[:user_id]).to eq user.id
end
end
end
在我的控制器中,我有:
In my controller I have:
def create
user = Authenticatable::User.find_by(email: params[:email])
if user && user.login(params[:password])
session[:user_id] = user.id
redirect_to main_app.root_path, notice: "You are signed in!"
else
flash[:error] = 'Invalid email or password'
redirect_to authenticatable.sign_in_path
end
end
我得到的 rspec 失败是:
The rspec failure I get is:
NameError:
undefined local variable or method `main_app' for #<RSpec::ExampleGroups::AuthenticatableSessionsController::GETCreate:0x007f9f583d6f18>
如何才能让 rspec 使用所有可用的路由助手.
How can I make it so that rspec will use all of the route helpers available to me.
我的 rails_helper.rb 文件如下:
My rails_helper.rb file is as follows:
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
require 'shoulda-matchers'
require 'pry'
require 'database_cleaner'
Rails.backtrace_cleaner.remove_silencers!
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
预先感谢您的任何帮助或建议.
Thanks in advance for any help or suggestions.
推荐答案
好像还有其他人遇到了类似的问题……我没有机会亲自尝试,但可以看看.
It looks like there are otherswith similar problems ... I didn't have a chance to try this out myself but take a look at.
def main_app
Rails.application.class.routes.url_helpers
end
main_app.root_path
这篇关于如何控制器测试 rails 4 引擎重定向回 main_app 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!