9

I am trying to use RSpec to functional test my REST apis.

The way I would LIKE it to work is using a CI build to build and deploy my application to a test server somewhere in the cloud and then have it kick off automated functional tests. But in order to properly do that, I need to be able to pass in the base url/domain for where the app was deployed. It will not be the same.

Everything I've found so far makes it seem like RSpec can't do this. Is there another way to do it if I can't pass in parameters on the command line? Or is RSpec not the right choice for this?

dfherr
  • 1,633
  • 11
  • 24
Kevin M
  • 2,717
  • 4
  • 26
  • 31
  • why do you need that? – Taryn East Sep 23 '14 at 00:40
  • Thought I described the scenario. I'd like a jenkins build that could build the app, run unit tests, then deploy the build to an ephemeral cloud server (new ec2 instance or something as to keep costs down, will shut down after tests complete) and then run tests. Since this will be a new server, I won't know the url to run the tests against. – Kevin M Sep 23 '14 at 14:31
  • not really a answer to the question, but did you try https://travis-ci.org/ ? dunno if they have ways to integrate such parameters – dfherr Sep 23 '14 at 16:19
  • You described the scenario, but you did not explain why "in order to properly do that, I need to be able to pass in the base url/domain for where the app was deployed" – Taryn East Sep 23 '14 at 23:46

1 Answers1

14

One way would be to bypass the call to rspec with something that accepts command line arguments and then initiate rspec in code. If you do not want to write your own binary for that, rake is capable of that too.

Look here for how to run rspec from code.

Another way would be setting an ENV variable when calling your test and preferably making it optional in the specs.

$> SPEC_URL=http://anotherhost:666 rspec

in code:

url = ENV['SPEC_URL'] || "http://localhost:4000"

I would suggest method two as it's the cleaner and easier approach in my opinion.

John Donner
  • 524
  • 6
  • 14
dfherr
  • 1,633
  • 11
  • 24
  • Thanks. I thought I had mentioned it but didn't. I had come across the environment variable approach before. That seems like a hack to me but might end up having to be the only way. I will check out using rake and see how that would work. – Kevin M Sep 23 '14 at 14:33
  • If you use TeamCity then write: `url = ENV['SPEC_URL'].to_s.empty? ? ENV['SPEC_URL'] : "http://localhost:4000"` ENV['SPEC_URL'] can be not only nil but also an empty string (in case with TeamCity). – Evmorov Nov 03 '16 at 12:44
  • If dealing with potential empty strings it's easier to do this: `url = ENV['SPEC_URL'].presence || "http://localhost:4000"` – JP Duffy Dec 13 '22 at 19:14