当我运行rake test
时,我收到以下错误:
[注]
您可能在Ruby解释器或扩展库中遇到过错误。
欢迎使用错误报告。 有关详细信息:http://www.ruby-lang.org/bugreport.html
中止
我的应用程序中有3个控制器,其中两个是模型。第三个控制器只是我用来处理主视图(主界面)而不是模型的控制器。
我将第3个模型生成为独立控制器,但其他两个控制器是我生成的支架的一部分。 (客户和项目)
这是我的客户控制器测试:
require 'test_helper'
class ClientsControllerTest < ActionController::TestCase
setup do
@client = clients(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:clients)
end
test "should get new" do
get :new
assert_response :success
end
test "should create client" do
assert_difference('Client.count') do
post :create, client: { name: @client.name, email: @client.email + "create" }
end
assert_redirected_to client_path(assigns(:client))
end
test "should show client" do
get :show, id: @client
assert_response :success
end
test "should get edit" do
get :edit, id: @client
assert_response :success
end
test "should update client" do
patch :update, id: @client, client: { name: @client.name }
assert_redirected_to client_path(assigns(:client))
end
test "should destroy client" do
assert_difference('Client.count', -1) do
delete :destroy, id: @client
end
assert_redirected_to clients_path
end
end
项目控制器测试:
require 'test_helper'
class ProjectsControllerTest < ActionController::TestCase
setup do
@project = projects(:one)
@client = clients(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:projects)
end
test "should get new" do
get :new
assert_response :success
end
test "should create project" do
assert_difference('Project.count') do
post :create, project: { client_id: @client, project_description: @project.project_description, project_timescale: @project.project_timescale, title: @project.title }
end
assert_redirected_to project_path(assigns(:project))
end
test "should show project" do
get :show, id: @project
assert_response :success
end
test "should get edit" do
get :edit, id: @project
assert_response :success
end
test "should update project" do
patch :update, id: @project, project: { client_id: @project.client_id, project_description: @project.project_description, project_timescale: @project.project_timescale }
assert_redirected_to project_path(assigns(:project))
end
test "should destroy project" do
assert_difference('Project.count', -1) do
delete :destroy, id: @project
end
assert_redirected_to projects_path
end
end
指数控制器测试:
require 'test_helper'
class IndexControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
test "should get contact" do
get :contact
assert_response:success
assert_template layout: 'application'
assert_select'title', 'My Notes'
assert_select'h1', 'Contact Us'
assert_select 'p', 'Complete the following form to get in touch with us.'
end
end
客户端模型测试:
require 'test_helper'
class ClientTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
#tests to see if an empty client can be created
test "no empty clients!" do
client = Client.new
client.save
refute client.valid?
end
#checks if a valid client can be created
test "should save valid clients" do
client = Client.new
client.name = "David"
client.email = "example@gmail.com"
client.save
assert client.valid?
end
test "no duplicate emails" do
client1 = Client.new
client1.name = "David"
client1.email = "example@gmail.com"
client1.save
assert client.valid?
client2 = Client.new
client2.name = "David"
client2.email = "example@gmail.com"
client2.save
refute client.valid?
end
end
项目模型测试:
require 'test_helper'
class ProjectTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
setup do
@client = clients(:one)
end
test "no empty projects" do
project = Project.new
project.save
refute project.valid?
end
test "make valid projects" do
project = Project.new
project.title = "first project"
project.project_description = "building a forum for a game clan"
project.project_timescale = "1 month"
project.save
assert project.valid?
end
end
clients.yml:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
email: MyText1
two:
name: MyString
email: MyText2
projects.yml:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
client: one
project_description: MyText
project_timescale: MyString
title: MyString
two:
client: two
project_description: MyText
project_timescale: MyString
title: MyString