Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 带有路由的Rails验证排除_Ruby On Rails_Ruby_Rspec_Rake_Rspec Rails - Fatal编程技术网

Ruby on rails 带有路由的Rails验证排除

Ruby on rails 带有路由的Rails验证排除,ruby-on-rails,ruby,rspec,rake,rspec-rails,Ruby On Rails,Ruby,Rspec,Rake,Rspec Rails,我想测试一个用户是否无效,如果用户名等于第一个路由块之一。 目前,我在rspec中对其进行了如下说明: it "is not valid with a excluded username" do `bundle exec rake routes`.scan(/\s\/(\w+)/).flatten.compact.uniq.each do |route| user.username = route user.should_not be_valid end end 问题是

我想测试一个用户是否无效,如果用户名等于第一个路由块之一。 目前,我在rspec中对其进行了如下说明:

it "is not valid with a excluded username" do
  `bundle exec rake routes`.scan(/\s\/(\w+)/).flatten.compact.uniq.each do |route|
    user.username = route
    user.should_not be_valid
  end
end

问题是,这降低了我的规范的速度,如果排除了每个第一个路由元素,有没有更好的方法来规范呢?

降低规范速度的是
bundle exec rake routes
,因为它会再次加载rails环境。为了防止这种情况,您可以从实际的rake任务中提取显示路由的代码(可以在中找到)。通过这些修改,您的规范可能会如下所示:

it "is not valid with a excluded username" do
  Rails.application.routes.routes.map(&:path).join("\n").scan(/\s\/(\w+)/).flatten.compact.uniq.each do |route|
    # blahblah
  end
end

降低规范速度的是
bundle exec rake routes
,因为它会再次加载rails环境。为了防止这种情况,您可以从实际的rake任务中提取显示路由的代码(可以在中找到)。通过这些修改,您的规范可能会如下所示:

it "is not valid with a excluded username" do
  Rails.application.routes.routes.map(&:path).join("\n").scan(/\s\/(\w+)/).flatten.compact.uniq.each do |route|
    # blahblah
  end
end