3
我有一个生成新用户的rake任务。需要通过命令行输入电子邮件,密码和password_confirmation(确认)的值。Rspec Rake任务:如何解析参数?
这是我的耙子任务的代码:从我的命令行
namespace :db do
namespace :setup do
desc "Create Admin User"
task :admin => :environment do
ui = HighLine.new
email = ui.ask("Email: ")
password = ui.ask("Enter password: ") { |q| q.echo = false }
confirm = ui.ask("Confirm password: ") { |q| q.echo = false }
user = User.new(email: email, password: password,
password_confirmation: confirm)
if user.save
puts "User account created."
else
puts
puts "Problem creating user account:"
puts user.errors.full_messages
end
end
end
end
我可以通过键入“管理员:设置耙分贝”调用它。
现在我想用rspec测试这个任务。 到目前为止,我设法创建下面的spec文件:
require 'spec_helper'
require 'rake'
describe "rake task setup:admin" do
before do
load File.expand_path("../../../lib/tasks/setup.rake", __FILE__)
Rake::Task.define_task(:environment)
end
let :run_rake_task do
Rake.application["db:setup:admin"]
end
it "creates a new User" do
run_rake_task
end
end
当运行规范的我rake任务会要求输入从我的命令行。所以我需要的是解析电子邮件,密码和确认值,以便在执行我的规格时,耙子任务不会要求这些字段的值。
如何从spec文件实现此目的?
你会如何解决这个错误? NameError: 未初始化的常量HighlLine #./spec/lib/tasks/data_rake_spec.rb:10:in' –