如何在 Rails/Capybara/Cucumber 或 Rspec 中测试帖子 [英] How to test posts in Rails / Capybara / Cucumber or Rspec

查看:25
本文介绍了如何在 Rails/Capybara/Cucumber 或 Rspec 中测试帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rspec、cucumber 和 capybara,我正在寻找一种方法来测试恶意用户无法破解表单然后发布到他/她没有权限的网址.我在 cancan 中设置了我的权限,这样这应该"起作用,但是,我可以测试它的唯一方法是自己破解表单.

I'm using rspec, cucumber and capybara and I'm looking for a way to test that a malicious user can't hack a form then post to an url he/she doesn't have permission to. I have my permissions set up in cancan such that this "should" work, however, the only way I can test it is by hacking a form myself.

如何使此类测试自动化?使用 webrat,我可以使用 rspec 进行单元测试,例如

How can I automate this sort of testing? With webrat I could do this in a unit test with rspec with something like

put :update, :user_id => @user.id, :id => @user_achievement.id
response.should contain("Error, you don't have permission to access that!") 

然而,在水豚中,似乎只能访问.我找不到办法做到这一点,我到处搜索.

In capybara, however, visit only does get's it seems. I can't find a way to do this, I've googled everwhere.

任何帮助将不胜感激,谢谢

Any help would be much appreciated, Thanks

推荐答案

我认为你可以通过 rack-test 做到这一点https://github.com/brynary/rack-test

I think you can do this with rack-test https://github.com/brynary/rack-test

在您的 Gemfile 中:

in your Gemfile:

gem 'rack-test'

在你的 env.rb 文件中

in your env.rb file

module CapybaraApp
  def app; Capybara.app; end
end
World(CapybaraApp)
World(Rack::Test::Methods)

某处的步骤定义:

When /^I send a POST request to "([^"]*)"$/ do |path|
  post path
end

我学到的大部分知识来自这里:http://www.anthonyeden.com/2010/11/testing-rest-apis-with-cucumber-and-rack-test

Most of what I learned came from here: http://www.anthonyeden.com/2010/11/testing-rest-apis-with-cucumber-and-rack-test

更新:我认为您可以使用较新版本的 Rails 和/或 Cucumber 跳过对 env.rb 文件的更改(不确定是哪个,我只是不在我的较新项目中执行该部分,而且效果很好)

UPDATE: I think you can skip the changes to your env.rb file with newer versions of Rails and/or Cucumber (not sure which, I just don't do that part on my newer projects and it works fine)

这篇关于如何在 Rails/Capybara/Cucumber 或 Rspec 中测试帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆