1

I'm currently creating rspec test cases for a Rails 2.3 application. The problem I'm confronting is that the application has a dependency on data from an external database like this:

  • Main_User is an external application.

  • Node_User is the application I'm testing.

When a new Node_User is created, Node_User calls Main_User which creates a user and returns with some data which Node_User uses to create a the Node_User.

Is there any way I can query the Main_User database to verify that the record created there contains the data that is passed to Node_User? Or is there a better way to test this?

Thank you!

2
  • Are you writing unit tests or integration tests? Commented Jan 4, 2012 at 23:23
  • I guess it would be more integration testing. The call to Main_User app is done inside a the Node_User model as a Net::HTTP call. We send Main_User a few parameters. It then creates a Main_User record and sends back the information for Node_User The data is then massage a little and finally the Node_User record is created. I want to make sure that what we get back, is Main_User is creating on it's end from the parameters sent to Main_User. Commented Jan 4, 2012 at 23:38

1 Answer 1

1

Yes, you can theoretically query the remote database if you have access to the user/pass/hostname for that database. (Search for "Rails multiple databases.")

But I wouldn't necessarily recommend taking that approach. Your tests would be modifying the state of the remote database. This strikes me as unsafe. You'll be creating bogus records--and possibly deleting or corrupting data, if your tests do any mass deletes or exercise broken functionality. In other words, your tests may do bad things to a production database.

Therefore, I would recommend mocking the remote database in some way. One option would be VCR. It's really cool, but it would require you to operate on the remote database at least once, because it has to record the first run. Another option is to use one of the tools underlying VCR, such as FakeWeb.

Of course, this only tests the Node_User side of things. You said you also need to verify that the remote record is correct. To do that, I would recommend writing tests on the Main_User side. Such tests would simulate an incoming RESTful request from Node_User and verify the correct result.

That only works if Main_User is your application. If it's a third-party service, then there may not be a safe way to test it. You may just have to trust its correctness.

Sign up to request clarification or add additional context in comments.

2 Comments

I think i'll just take the second approach. Both applications belong to me and they're both in a localhost environment (never touching production data). but it will be more useful and safe to test them independently. Do you know a good approach to deal with seeding data and testing for consistency in Main_User?
Yes, I use Machinist (github.com/notahat/machinist) for that purpose. I also integrate it with Faker (faker.rubyforge.org), which I use to generate required model attributes that aren't important to the current test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.