My Rails项目的'spec'文件夹包含以下内容:
1)'controllers'子文件夹: a)'pages_controller'规范文件(带有相应的“描述PagesController do”语句):
require 'spec_helper'
describe PagesController do
render_views
before(:each) do
@base_title = "Ruby on Rails Tutorial Sample App | "
end
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title", content: @base_title+"Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title", content: @base_title+"Contact")
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_selector("title", content: @base_title+"About")
end
end
describe "GET 'help'" do
it "should be successful" do
get 'help'
response.should be_success
end
it "should contain the right title" do
get 'help'
response.should have_selector("title", content: @base_title+"Help")
end
end
end
b)'users_controller'规范文件(带有相应的“describe UsersController do”语句):
require 'spec_helper'
describe UsersController do
render_views
describe "GET '/new'" do
it "should be successful" do
get 'new'
response.should be_success
end
it "should have the right title" do
get 'new'
response.should have_selector("title", content: "Sign up")
end
end
end
2)'requests'子文件夹包含以下'layout_links'规范文件:
require 'spec_helper'
describe "LayoutLinks" do
it "should have a Home page at '/'" do
get '/'
response.should have_selector('title', content: 'Home')
end
it "should have a Contact page at '/content'" do
get '/contact'
response.should have_selector('title', content: 'Contact')
end
it "should have an About page at '/about'" do
get '/about'
response.should have_selector('title', content: 'About')
end
it "should have a Help page at '/help'" do
get '/help'
response.should have_selector('title', content: 'Help')
end
it "should have a signup page at '/signup'" do
get '/signup'
response.should have_selector("title", content: "Sign up")
end
end
我最初将以下代码添加到我的“users_controller”文件中:
it "should have a signup page at '/signup'" do
get '/signup'
response.should have_selector("title", content: "Sign up")
end
我运行时测试失败了,但是当我将它添加到'layout_links'规范文件中时,它就通过了。这告诉我,我遗漏了RSpec和/或集成测试的一些基本方面。如果有必要,我很乐意重新提出这个问题,使其更具普遍性。
答案 0 :(得分:0)
你的预感是正确的,这里有一些关于配置的约定。
控制器测试将在指定的控制器上运行。因此,PagesController
测试将会有效......好吧,PagesController
。因此,get 'home'
行将尝试在home
控制器中获取Pages
操作,Pages
控制器将承担所有这些测试。除非您的Pages
控制器执行signup
操作,否则会出错。
requests
类型测试用于集成测试,不承担控制器。