Har*_*ran 2 ruby integration-testing ruby-on-rails devise ruby-on-rails-4
我已经将Devise安装并集成到我的应用程序中,现在我正在构建其测试套件.我正在使用Rails 4.2.1附带的默认测试套件(Test :: Unit?),目前正在使用集成套件.
综合测试:
def setup
@user = User.create(email: "user@hlist.com",
encrypted_password: Devise.bcrypt(User, 'password1'))
@post = { title: "This is the title",
content: "Detailed comment."*10,
phone: 9991118888,
email: "email@hlist.com",
user_id: users(:spiderman).id }
@p = @user.posts.build(@post)
end
test "creates a new post successfully" do
sign_in_as(@user)
get new_post_path(@user)
assert_template 'posts/new'
assert_difference 'Post.count', 1 do
post posts_path, post: @post
end
assert_template 'posts/show'
end
Run Code Online (Sandbox Code Playgroud)
我还在test_helper.rb文件中创建了以下方法:
def sign_in_as(user)
post_via_redirect user_session_path, 'user[:email]' => user.email,
'user[:encrypted_password]' => Devise.bcrypt(User, 'password1')
end
Run Code Online (Sandbox Code Playgroud)
但是,运行测试时出现以下错误:
1) Failure:
PostsCrudTest#test_creates_a_new_post_successfully [/Users/harishramachandran/dropbox/documents/harish/coding/workspace/h_list/test/integration/posts_crud_test.rb:19]:
expecting <"posts/new"> but rendering with <[]>
Run Code Online (Sandbox Code Playgroud)
我在网上寻找解决方案,我能找到的只是涉及RSpec或Capybara的解决方案.这是一个我正在创建的应用程序,用于学习默认套件,然后再转到RSpec和Capybara在另一个应用程序中.有没有办法在Test :: Unit中解决这个问题?
在您创建帖子后,post posts_path, post: @post您没有被重定向到帖子/节目.键入follow_redirect!正确的,像这样经过:
post posts_path, post: @post
follow_redirect!
或者你也可以做post_via_redirect posts_path, post: @post,这将这样做.发布然后重定向到发布/显示.
一些注意事项 Devise.bcrypt 在几天前已被弃用.因此,将来如果您更新Gemfile,您将收到弃用错误.是的,您可以使用Rails默认测试进行集成测试.这是测试用户是否在登录时获取根路径的示例.
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
test "user can see home page after login" do
get user_session_path
assert_equal 200, status
@david = User.create(email: "david@mail.com", password: Devise::Encryptor.digest(User, "helloworld"))
post user_session_path, 'user[email]' => @david.email, 'user[password]' => @david.password
follow_redirect!
assert_equal 200, status
assert_equal "/", path
end
test "user can not see home page without login" do
get "/"
assert_equal 302, status
follow_redirect!
assert_equal "/users/sign_in", path
assert_equal 200, status
end
end
Run Code Online (Sandbox Code Playgroud)
用于测试的Rails类(ActiveSupport :: TestCase,ActionController :: TestCase,ActionMailer :: TestCase,ActionView :: TestCase和ActionDispatch :: IntegrationTest)从minitest断言继承断言,而不是测试单元.这适用于您的Rails版本.
| 归档时间: |
|
| 查看次数: |
1565 次 |
| 最近记录: |