0

我的 Rails 项目的“规范”文件夹包含以下内容:

1) 一个 'controllers' 子文件夹,其中: a) 一个 'pages_controller' 规范文件(带有相应的“describe 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) 具有以下“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 和/或集成测试的一些基本方面。如有必要,我很高兴重新措辞这个问题以使其更具普遍性。

4

1 回答 1

0

你的预感是正确的,这里有一些关于配置的约定。

控制器测试将在指定的控制器上运行。所以你的PagesController测试会成功......好吧,PagesController. 因此,该get 'home'行将尝试home在您的Pages控制器中获取操作,该Pages控制器被假定用于所有这些测试。除非你的Pages控制器有一个signup动作,否则它会出错。

类型测试用于集成测试,requests不假设控制器。

于 2013-07-28T22:15:14.443 回答