Functional Testing of a Restful Post in Ruby on Rails

Ruby on Rails functional testing with the RESTful Authentication plugin

Faking it is perfectly acceptable.

However, write other tests that ensure that the things you want protected are protected. So

test "it should show the profile page" do
user = Factory(:user)
login_as(user)
get :show, :id => user
assert_response :success
end

test "it should not show the profile page cos I'm not logged in" do
user = Factory(:user)
get :show, :id => user
assert_response :redirect
end

Feel free to hit me up for followups!

Why did post fail in my Rails functional test?

Add the following assertion:

assert_nil assigns(:user).errors

Which will fail if there were errors saving your object (perhaps a validation), and show you the value of the errors object.

How can I test a Rails RESTful API post request with xml as input?

The XML parameters parser was removed from core in Rails 4.0. This was due to several vulnerabilities and the fact that most API´s worth using today are JSON or moving towards JSON.

The removed functionality is available from the actionpack-xml_parser gem.

Testing RESTful API with Cucumber in a front end less application

I think Webrat is more than what you need.
For XML feed testing, you don't need a browser simulator like Webrat which would load pages and analyse all the markup (links, forms etc.) when you really don't have any HTML pages.

You rather need something like Curl (http://curl.haxx.se) or Curb (on rubyforge, which are ruby bindings for Curl), or Patron (on rubyforge).

These libraries can make a request header as per your liking (e.g. setting Content-Type, choosing among GET PUT POST DELETE HEAD etc.) and obtain the response, and probably follow 302 redirections when needed.

The response returned, can be then turned into XML object, and XML parsers available for Ruby can be used to test the output. Also, you can write XMLMapping classes (on rubyforge) to convert XML output into Ruby objects and test their attributes etc. This is much cleaner, IMHO.

How can I unit-test a method that uses a REST API with RSpec?

Yes, it's appropriate for tests to hit external services, but you can minimize the impact of that on your test suite in a couple of ways.

I would test this code as follows:

  • Register a Facebook test user.
  • Write RSpec feature specs or Cucumber scenarios to test the entire feature that uses Facebook through the entire stack, using the test user. Use the VCR gem to record Facebook's responses so that the remote calls don't slow down your tests.
  • Write RSpec specs (unit tests) for SocialNetwork.

    • In one or a few unit tests per different call to Facebook, let them hit Facebook using the test user and, again, use VCR to keep them fast. Optionally, let one or a few of them hit Facebook all the time so that you know if Facebook's API changes.
    • In the rest of your specs of SocialNetwork, ones that just test variations of calls that you already tested, stub out Koala.

    It's hard to explain exactly which tests should hit Facebook and which should use stubs without having them in front of us. See how it goes and post again if you need more advice.

End-to-end testing a RESTful Web service (Rails)

This might be of interest to you: http://groups.google.com/group/ruby-capybara/browse_thread/thread/5c27bf866eb7fad3

What you might want to to try is combining Cucumber (or similar) with one of the tools mentioned in the link above, so you can do something like

Given I have 2 posts
When I send "DELETE" to post with id 1
Then I should have 1 post

This way you can test the API's full stack and check the result.



Related Topics



Leave a reply



Submit