Rails 测试设计控制器的简单示例
Simple example of Rails Testing Devise Controllers
我想要一个最简单的例子来测试新注册(电子邮件、密码、密码确认)的设备操作属性的有效性,sign_in 和 forgot_password。互联网上到处都是 RSpec,但我想使用 Rails 4.2 给我的东西,但绝对没有。
我坚持使用默认实现:
require 'test_helper'
class RegistrationsControllerTest < ActionController::TestCase
def setup
@controller = RegistrationsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = Registrations.new(username: "John", email: "myemail@email.com")
end
end
我知道这不算太多,但我正在迈出 TDD 的第一步,所以请不要开枪!
如何检查用户属性的有效性,例如电子邮件地址为零或密码为 100 个字符?
您可以检查 rails 内置 minitest
我假设你有压倒一切的设计registration_controller。
在控制器的 tdd 中,您可以编写测试用例以执行操作。
检查修改后的代码:-
require 'test_helper'
class RegistrationsControllerTest < ActionController::TestCase
include Devise::TestHelpers # for including devise's actions
def setup # this set up default settings for controller
@controller = RegistrationsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = Registrations.new(username: "John", email: "myemail@email.com")
end
setup do # this used for setting global variable used in test file
@user= users(:one) # users is the fixture file of test in which you can set default data for test environment.
end
test "should create user" do # then you test cases for controller
sign_in users(:one)
post :create, users:{email:'test@test.com, password:'XXXX'...}# you can pass arguments for create method. Please check it once, i am not sure about names
assert_response :success
end
end
Rails minitest 提供了一些 assertion.
我想要一个最简单的例子来测试新注册(电子邮件、密码、密码确认)的设备操作属性的有效性,sign_in 和 forgot_password。互联网上到处都是 RSpec,但我想使用 Rails 4.2 给我的东西,但绝对没有。
我坚持使用默认实现:
require 'test_helper'
class RegistrationsControllerTest < ActionController::TestCase
def setup
@controller = RegistrationsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = Registrations.new(username: "John", email: "myemail@email.com")
end
end
我知道这不算太多,但我正在迈出 TDD 的第一步,所以请不要开枪!
如何检查用户属性的有效性,例如电子邮件地址为零或密码为 100 个字符?
您可以检查 rails 内置 minitest
我假设你有压倒一切的设计registration_controller。
在控制器的 tdd 中,您可以编写测试用例以执行操作。 检查修改后的代码:-
require 'test_helper'
class RegistrationsControllerTest < ActionController::TestCase
include Devise::TestHelpers # for including devise's actions
def setup # this set up default settings for controller
@controller = RegistrationsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env["devise.mapping"] = Devise.mappings[:user]
@user = Registrations.new(username: "John", email: "myemail@email.com")
end
setup do # this used for setting global variable used in test file
@user= users(:one) # users is the fixture file of test in which you can set default data for test environment.
end
test "should create user" do # then you test cases for controller
sign_in users(:one)
post :create, users:{email:'test@test.com, password:'XXXX'...}# you can pass arguments for create method. Please check it once, i am not sure about names
assert_response :success
end
end
Rails minitest 提供了一些 assertion.