I am new to Ruby and have been asked to use it in our new project. We have also been asked to use Padrino (Sinatra) as backend/framework. For testing we have been asked to use Rspec. I have been hunting for tutorials for long that would guide in using Rspec for Ruby on Padrino. What I get is mostly with reference to RoR. But, I am in need of Ruby on Padrino.
Please guide me for the same with any starters/guides/references/discussions, etc.
Please correct me, if I am wrong anywhere. May be I haven't searched with the right combination of words/phrases for my issue.
I am using Ruby 1.9.3 and Padrino v.0.10.6.
Note : I have also referred the SO question, but it didn't help.
I never used Padrino, but it seems that it isn't much different from Sinatra.
I suggest reading Sinatra and RSpec resources.
You can get started with this:
How to use rspec in a Sinatra Application
Sinatra, RSpec and DataMapper: Configuring and using a database for tests
And by reading specs that were written by other people on GitHub. These are some of mine, but they are not the cleanest thing that exists.
EDIT: a short tutorial
Getting started with this framework is much quicker and easier than with Sinatra. :)
Install Padrino: gem install padrino
Create an application: padrino g project myapp -d datamapper -t rspec
The command speaks for itself. :)
Run the tests: rspec --color
No tests were found, obviously. Let's create one in spec/hello/hello_spec.rb:
require File.dirname(__FILE__) + "/../spec_helper.rb"
describe "get '/'" do
it "should display hello world" do
get '/'
last_response.body.should == "Hello world!"
end
end
Run the tests again.
They failed, because no route get '/' exists. Let's create it.
In app/controllers/hello.rb:
Myapp.controller do
get '/' do
"Hello world!"
end
end
Run the test: it passes!
Check Padrino's documentation for more information and cool features, such as the controllers generator and the admin interface.
Good luck!
Related
Recently I made a simple ruby application and have been using minitest to test it.
Following the advice of the Head First Ruby book, I automated this testing using Rake(I'll write what it told me to put in the Rakefile at the end of this post, in case that helps). The test seems to run fine (everything passes in a way I would expect it to), but I always get this notification at the end of it all:
rvm/gems/ruby-2.3.0/gems/guard-2.14.0/lib/guard/notifier.rb:28: warning: instance variable #notifier not initialized
Testing things manually by telling ruby to include which files I want, does not have this issue, only when I use "rake test" to test things.
As far as I can tell, this is related to when I set up Guard when I was following Michael Hartl's Rails Tutorial, at the end of chapter 3. I followed the directions for setting that up (correctly, as far as I can tell), and this was all in a completely different folder(ultimately my ruby and rails projects do have the same parent folder that they sit in, but are themselves in completely separate ruby_projects and rails_projects folders). If possible, I would like to stop this notification on my ruby application that I am testing. Is there a good way to do this?
Contents of the Rakefile I am using, if that helps:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "lib"
t.test_files=FileList['test/**/test_*.rb']
end
My test file requires minitest/autorun, and the file for the application that I am testing, then has the normal tests
Seems like there's some weird conflict...
The reason is that Guard::Notifier.connect isn't connected. Normally, when you run guard, Guard.setup is called which does this.
If you're not using guard (e.g. interactively), then calling the following from your Rakefile should work around the problem:
Guard::Notifier.connect(notify: false, silent: true)
Guard::Notifier.disconnect
This will initialize the variable.
For a faster response, always report such issues on the project page on Github. If you can share the project where this occurs, maybe a better fix is possible. (It's best to provide a repository, since it really speeds up fixing things and often errors like this are very hard to simulate without the exact code).
I'm experimenting, and I'm trying to launch dummy Sinatra application from RSpec and kill it when the spec is finished. Something like:
# spec/some_spec.rb
before(:all)
# launch sinatra dummy app
end
after (:all)
# kill sinatra dummy app
end
it 'should return list of whatever' do
expect(JSON.parse(make_request('0.0.0.0:4567/test.json')))
.to include('whatever')
end
I could use system("ruby test/dummy/dummy_app.rb"), but how can I kill that process only? Does anyone know how I can launch the Sinatra inside a test (or from another ruby script)? I know about WebMocks, but I want to see if I can manage to make my test work this way.
Look under RSpec on "Testing Sinatra with Rack::Test". I'd suggest you use that code as boilerplate to get started.
Just add this to your describe block:
def app
Sinatra::Application
end
I would suggest you read up RSpec.
Since you want to test an external system, by the looks of your comment, instead of system "curl whatewer.com", you can use Net::HTTP to make requests and then test against the response.
Have a look at "Testing an external API using RSpec's request specs".
As I'm writing request specs to ensure the features won't be broken I decided to rather write separate Cucumber features. The nice thing is that I can use Capybara, and thanks to Selenium Web Drive, I can launch a server before I run my tests.
So, I created a dummy Sinatra application (that will represent the external service to which the actual code I'm testing is doing requests (including a nasty system('curl whatever.com')).
All I have to do is stub out the methods passed to curl to use Capybara.current_session.server.host and Capybara.current_session.server.port.
Once I'm done with my re-factoring all I have to do is remove the Capybara server variables, and Selenium web drive from the cucumber/capybara configuration.
Tests after a brief change will be still working and will be valid.
Update
In the end I wrote it all with RSpec request tests, as doing it in Cucumber was little bit time consuming and I already spend too much time on this.
I mark these kind of request tests with RSpec tag and Before I lunch these I manually lunch simple Sinatra/Grape dummy API application to which the request are made. (Then I run RSpec tests with this tag)
So basically I end up with specs for functionality that uses net/http that uses WebMock and don't need a server, and request tests for which I need to run the server before I run the specs. So the original question remains, how to lunch a server before tests start
After I cover all the functionality I'm gonig to rewrite the curl to net/http however I'm going to keep those requests specs as I discovered they are nice idea when it comes to crazy API scenarios (like testing https + diggested authentication)
I created an engine inside an app that depends on an external gem: Spree.
And I did my best to set RSpec within that engine following this guide.
When I try testing a controller with some integrated testing:
tests_spec.rb:
require 'spec_helper'
describe "Tests" do
describe "GET /tests" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get tests_path
response.status.should be(200)
end
end
end
I get an error in the dummy app's environment.rb file, that says that following command couldn't be run:
# Initialize the rails application
Dummy::Application.initialize!
Because my controller is using Admin::ResourceController as an extension which is declared within the external Spree gem. As if rspec is unable to read that class from gemfile of the main app (the one that contains the engine plugin).
Can someone please shed some light on that?
How can I make a webservice in ruby that interact with ruby web application.
Thanks!
For simple stupid web service you can't go past Sinatra
http://www.sinatrarb.com/
The cannonical example.
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end
That's it
You have not given us a lot to go on here however it is likely that you are interested in one or more of the following. Try googling them to see which one will do what you want
ActiveResource
ActionWebService
Savon/Handsoap
I am trying to call a helper method in my Sinatra application from irb in order to test its output. I know I can get a console using this tip, and I've tried racksh as well. But if I do a "defined? my_helper" I always get nil. There must be some simple way of getting at those helpers. I have a feeling that this means digging through the architecture of Rack a little bit. Any hints?
You can of course test your Sinatra helpers via IRB.
Suppose you had a modular Sinatra app with a helper method foo that printed "baz":
require "my-app.rb"
MyApp.new.helpers.foo # => "baz"
I am trying to call a helper method in my Sinatra application from irb in order to test its output
Instead of testing it over the command line, use RSpec. See How can I test helpers blocks in Sinatra, using Rspec?