我编写了一个基本测试,以确保页面和标题按预期呈现。
static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should_get_contact" do
get :contact
assert_response :success
assert_select "title", "Contact"
end
end
路线.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
end
联系人.html erb
<% provide(:title, "Contact")%>
<h1>Help</h1>
<p>
Some text.
</p>
应用程序.html erb
<!DOCTYPE html>
<html>
<head>
<title><%= yield(:title) %></title>
...
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
这是我的
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
end
正如你们注意到的那个样,并没有“接触”动作,所以我希望考试不会通过,而我却一直得到绿灯。
davide@davidell:~/Desktop/app/sample_app$ bundle exec rake test
Run options: --seed 62452
# Running:
....
Finished in 0.194772s, 20.5369 runs/s, 41.0737 assertions/s.
4 runs, 8 assertions, 0 failures, 0 errors, 0 skips
为什么?我错过了什么?非常感谢您的回答,新年快乐:)