Как тестировать сообщения в Rails/Capybara/Cucumber или Rspec
Я использую rspec, огурец и capybara, и я ищу способ проверить, что злоумышленник не может взломать форму, а затем отправить URL-адрес, на который у него нет разрешения. У меня есть мои разрешения, установленные в cancan, так что это "должно" работать, однако, единственный способ проверить это - взломать форму самостоятельно.
Как я могу автоматизировать такое тестирование? С webrat я мог бы сделать это в unit test с rspec с чем-то вроде
put :update, :user_id => @user.id, :id => @user_achievement.id
response.should contain("Error, you don't have permission to access that!")
В capybara, однако, посещение только кажется, кажется. Я не могу найти способ сделать это, я googled везде.
Любая помощь будет высоко оценена,
Благодаря
Ответы
Ответ 1
Я думаю, вы можете сделать это со стойкой
https://github.com/brynary/rack-test
в вашем Gemfile:
gem 'rack-test'
в файле env.rb
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
UPDATE: Я думаю, что вы можете пропустить изменения в файле env.rb с более новыми версиями Rails и/или Cucumber (не уверен, что я просто не делаю эту часть в моих новых проектах, и она отлично работает)
Ответ 2
То же, что и @Josh Crews. Я в основном основывал это: http://www.anthonyeden.com/2010/11/testing-rest-apis-with-cucumber-and-rack-test/#comment-159. Но есть два примечательных исключения: 1) Я проверяю фактическое тело ответа, 2) я демонстрирую, как тестировать запрос POST. Вот пример использования Rails 3.0.9:
Шаги:
# features/step_definitions/api_step.feature
When /^I send a GET request to "([^\"]*)"$/ do |url|
authorize(User.last.email, "cucumber")
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
get url
end
When /^I send a POST request to "([^\"]*)" with:$/ do |url, body|
authorize(User.last.email, "cucumber")
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
post url, body
end
Then /^the JSON response should have (\d+) "([^\"]*)" elements$/ do |number_of_children, name|
page = JSON.parse(last_response.body)
page.map { |d| d[name] }.length.should == number_of_children.to_i
end
Then /^I should receive the following JSON response:$/ do |expected_json|
expected_json = JSON.parse(expected_json)
response_json = JSON.parse(last_response.body)
response_json.should == expected_json
end
Then /^I should receive the following JSON object response:$/ do |expected_json|
expected_json = JSON.parse(expected_json)
response_json = JSON.parse(last_response.body)
if expected_json['id'] == 'RESPONSE_ID'
expected_json['id'] = response_json['id']
end
response_json.should == expected_json
end
Характеристика:
# features/api/some_feature.feature
Feature: Users API
Background:
Given the following users exist:
| id | name |
| 1 | Joe |
| 2 | Sue |
| 3 | Paul |
Scenario: Index action
When I send a GET request to "/users/"
Then the JSON response should have 3 "user" elements
And I should receive the following JSON response:
"""
[
{
"id":1,
"name":"Joe"
},
{
"id":2,
"name":"Sue"
},
{
"id":3,
"name":"Paul"
}
]
"""
Scenario: Create action
When I send a POST request to "/users/" with:
"""
{
"name":"Polly"
}
"""
Then I should receive the following JSON object response:
"""
{
"id":"RESPONSE_ID",
"name":"Polly"
}
"""
And I send a GET request to "/users/"
And the JSON response should have 4 "user" elements