尝试rspec-rails。我得到了一个奇怪的错误--假设没有找到路由,尽管在运行rails s时我可以在浏览器中很好地访问它们。
我甚至用just /尝试过
Failure/Error: get "/"
ActionController::RoutingError:
No route matches {:controller=>"action_view/test_case/test", :action=>"/"}不过,我绝对可以在浏览器中访问/和其他资源。在设置rspec时有没有什么我可能遗漏的东西?我将其放入Gemfile并运行rspec:install。
谢谢你,MrB
编辑:这是我的测试
1 require 'spec_helper'
2
3 describe "resource" do
4 describe "GET" do
5 it "contains /" do
6 get "/"
7 response.should have_selector("h1", :content => "Project")
8 end
9 end
10 end这是我的路由文件:
myApp::Application.routes.draw do
resources :groups do
resources :projects
end
resources :projects do
resources :variants
resources :steps
member do
get 'compare'
end
end
resources :steps do
resources :costs
end
resources :variants do
resources :costs
end
resources :costs
root :to => "home#index"
end我的spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.include RSpec::Rails::ControllerExampleGroup
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end我想,这并没有真正改变什么。
发布于 2011-05-04 05:34:29
据我所知,您正在尝试将两个测试合并为一个。在rspec中,这个问题应该分两步解决。在一个规范中测试路由,在另一个规范中测试控制器。
因此,添加一个文件spec/routing/root_routing_spec.rb
require "spec_helper"
describe "routes for Widgets" do
it "routes /widgets to the widgets controller" do
{ :get => "/" }.should route_to(:controller => "home", :action => "index")
end
end然后添加一个文件spec/controllers/home_controller_spec.rb,我使用的是由shoulda或extended定义的扩展匹配器。
require 'spec_helper'
describe HomeController do
render_views
context "GET index" do
before(:each) do
get :index
end
it {should respond_with :success }
it {should render_template(:index) }
it "has the right title" do
response.should have_selector("h1", :content => "Project")
end
end
end 实际上,我几乎从不使用render_views,但总是尽可能地隔离测试我的组件。视图是否包含我在view-spec中测试的正确标题。
我使用rspec分别测试每个组件(模型、控制器、视图、路由),并使用cucumber编写高级测试,对所有层进行切片。
希望这能有所帮助。
发布于 2011-04-27 22:56:49
为了进行控制器测试,您必须对控制器执行describe。此外,由于您是在控制器测试中测试视图的内容,而不是单独的视图规范,因此您必须使用render_views。
describe SomeController, "GET /" do
render_views
it "does whatever" do
get '/'
response.should have_selector(...)
end
endhttps://stackoverflow.com/questions/5804944
复制相似问题