我有一些麻烦控制器测试我创建的rails引擎。我遇到的问题是,我似乎无法让rspec使用main_app路由。
例如,我正在编写以下控制器测试。如果不是针对main_app,测试就会通过。Root_path返回nil。
我在控制器规范中的代码是:
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
在我的控制器中我有:
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失败是:
NameError:
undefined local variable or method `main_app' for #<RSpec::ExampleGroups::AuthenticatableSessionsController::GETCreate:0x007f9f583d6f18>
我怎样才能让rspec使用所有可用的路由帮助器呢?
我rails_helper。Rb文件如下:
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 }
提前感谢您的帮助和建议。
看起来还有其他人有类似的问题…我没有机会亲自尝试一下,但请看看。
堆栈溢出
def main_app
Rails.application.class.routes.url_helpers
end
main_app.root_path
直接放在控制器测试文件或rails帮助器中,就像单独的方法一样