【发布时间】:2015-05-27 19:29:29
【问题描述】:
我已安装 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
我还在 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
但是,我在运行测试时收到以下错误:
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 <[]>
我在网上寻找解决方案,但我能找到的只是涉及 RSpec 或 Capybara 的解决方案。这是我创建的一个应用程序,除其他外,在我在另一个应用程序中转到 RSpec 和 Capybara 之前学习默认套件。有没有办法在 Test::Unit 中解决这个问题?
【问题讨论】:
-
我认为你不应该将@user 传递给
get new_post_path(@user),因为get new_post_path 只是一个表单。创建帖子时,创建操作应负责创建与当前用户关联的帖子。试试get new_post_path -
我刚刚尝试过,它给了我同样的错误。
-
是否可以将 Unit::Test 集成测试与设计一起使用?还是我应该坚持使用控制器和模型测试?
标签: ruby-on-rails ruby ruby-on-rails-4 devise integration-testing