投票功能测试与用户认证实现
1. 投票功能测试
1.1 测试投票存储
为了测试投票功能的基本操作,我们需要在 test/controllers/votes_controller_test.rb 文件中添加测试代码,同时删除 Rails 自动生成的测试代码。以下代码用于确认新投票是否被正确存储:
class VotesControllerTest < ActionController::TestCase
test "creates vote" do
assert_difference 'stories(:two).votes.count' do
post story_votes_path(stories(:two))
end
end
end
上述测试使用 assert_difference 进行前后检查,以确认该操作是否正确修改了数据。具体步骤如下:
1. assert_difference 'stories(:two).votes.count' do :设置要检查的投票数量。
2. post story_votes_path(stories(:two)) :使用 HTTP POST 方法提交投票。
3. assert_difference 会确认提交的投票是否成功存储,并与相应的故事关联。
1.2 测试 Ajax 投票
超级会员免费看
订阅专栏 解锁全文
1116

被折叠的 条评论
为什么被折叠?



