Testing Angular with Protractor on a Sinatra server? - ruby

I am curious as to how best to set up a testing environment for Angular on a server that is running Sinatra. Normally when running a node server, you have a package.json which can require the dev-dependency of Protractor. However a Sinatra server will only have a Gemfile. Are there any best practices or gems that facilitate using Protractor in a Ruby environment?

Protractor is a node application so you must have node install to use it. In your case it's sounds like you will need to have both the Gemfile to maintain the dependencies for the Sinatra app and a package.json to manage dependencies for your automation tests Protractor.
You have lots of other options for end to end tests in ruby if you don't want to or don't have to use Protractor you can use rspec (the syntax for rspec and jasmine is similar) or cucumber as your testing framework and then use capybara (or selenium directly if you prefer) to control a browser and complete end to end tests.

Related

Can I use rack-test for deployment tests?

I've built a simple api in Sinatra, with the purpose of setting up a deployment pipeline using Docker. I'm at a stage where I could easily switch Sinatra for something else and there's currently one reason why I might do so.
I once wrote an api using Express and it was trivial to reuse the tests to test a deployment:
# Testing the code
chai.request(app)
.get('/')
# Testing a deployment
chai.request('http://localhost:8080')
.get('/')
Examples from: https://github.com/chaijs/chai-http#integration-testing
Now I am wondering if I can accomplish the same with rack-test and Sinatra. Simply sending a URL, instead of the app, crashes. So is there an easy way to accomplish this? I suppose I could write a testing framework on top of rack-test, but I'm not sure it's worth it, even though I do prefer Ruby over Javascript and Sinatra over Express.
I realised that I should be able to write a rack app that forwards all requests to the environment I want to run deployment tests against. So I went to Google and found a gem that does just that: rack-proxy
Here's how to write a simple rack app that redirects requests to your server:
require 'rack/proxy'
class Foo < Rack::Proxy
def rewrite_env(env)
env["HTTP_HOST"] = "api.example.com"
env
end
end
Then I ran my tests against Foo.new they succeeded. I checked the logs of that environment and I can confirm that my tests were in fact running against that environment.
Foo may not be the best name for such a proxy and you may not want the host name hardcoded, but I'm sure you can figure out how to make this work in your project if you need it.

How to stub and mock interactive ruby app with Cucumber?

I have an interactive CLI app based on Highline gem. I can run it interactively for Cucumber tests using Aruba. But I can't using stubs and mocks, because Aruba starts my app as a child process. If I try to use Aruba::InProcess feature, it loses interactivity.
I have no idea any more. In what way can I testing such app?
What exactly do you want to stub and mock?
Cucumber is primarily used for integration testing, i.e. testing from end-user's point of view. End-users use the app through its interface, that's why cucumber does not provide easy ways of stubbing and mocking the app's internals and you should not do it either, at least not with cucumber.
Consider using fixtures for cucumber or unit testing with rspec.
If you want to stub out responses from 3rd parties, then you can use webmock gem to intercept requests and return fixtures as a response or fakefs to do the same for the filesystem.
Ok, I take that: Cucumber is not about stubs and mocks. And interactive CLI apps is, probably, the best example for it.
So, while you need interactivity, Cucumber through Aruba starts your app in a child process. And the only way for affect it, I find, is environment variables usage. For example, by setting variable with values 'production'/'development'/'test' I can change configuration of my app to using test DB instead of production etc.

Typical way of hosting a Ruby WebSocket service that uses gems?

While developing JavaScript apps, I usually create an API app, totally separate from the UI app. For the API, I usually use Sinatra.
I'm developing a JavaScript app that will use a WebSocket service I build. I'd like to use Ruby (em-websocket for now) and ActiveModel for data models. I want to keep this really lightweight, like a Sinatra app is for a RESTful API.
It seems my WebSocket service will simply be a ruby script invoked via "ruby web_socket_service.rb". I'd like to be able to use various gems (like activerecord, capistrano, and nokogiri) with this WebSocket service. What's the most typical way of accomplishing this?
Would I be better off creating a standalone gem to contain my models and the WebSocket service script and then host my WebSocket service from that? Or maybe simply include the gems and models directly in the script via "gem 'name'? Or, is there some special library or framework commonly-used to tackle this?
Look at a Rails app. That's the approach I would take if your WebSocket service starts to grow towards a medium-sized app. I.e. bin, lib, Rakefile, and a Gemfile for your gems and bundler.
For smaller apps you can still use a Gemfile and bundler to manage the included gems. This locks gem versions so you won't have conflicts if you deploy to other servers. And then just put everything into one or two script files, similar to Sinatra.
Creating standalone gems is really only useful for libraries or application parts that are reusable across many applications. This doesn't sound like that sort of thing.

How do I run a Sinatra application on G-WAN?

I'm trying to write a Ruby web application and I want to use the functionality offered by Sinatra.
I used this code:
require 'rubygems'
require 'sinatra'
get '/' do
'Hello, world!'
end
I typed localhost:8080/?hello.rb in my browser and I received an error message that the script is unable to find sinatra.
I also tried to run the sinatra app using this command:
ruby hello.rb
and this time it worked!
So I suspect the problem is not in my code, but in the way I'm using G-WAN to serve the Ruby script.
Sinatra expects to be run by a rack-compatible server, which G-Wan is apparently not. When you run script manually it is being run by a WEBrick server that comes bundled with Ruby. Examples of compatible servers are thin, unicorn or apache/nginx with phusion passenger
Ruby (like Java or PHP) probably uses configuration files and/or environment variables to specify where to find packages, modules and libraries.
While G-WAN received similar requests for asm, C, C++, D, Objective-C, Java and C# support, Ruby users did not manifest interest in this area - so far.
If you point to us the relevant Ruby documentation, we will try to add this Ruby feature (just like it was done for the other languages).

How to use activerecord alone?

I prepare to develop one project, which has no UI. The project just need to interact with database, so is there any example for reference ?
Does your app have ties to a Rails-based app and need to read the Rails configuration files? Or, is it entirely stand-alone and have no Rails interaction?
ActiveRecord is OK for that, but if I don't need Rails compatibility I use Sequel. It's a great ORM that I find to be much more flexible.
If you need Rails compatibility and want to use ActiveRecord, look into using rails runner. From the docs:
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
Rails runner is for command-line apps that don't need the HTTPd server or user-interface of Rails. I use them for things like an app that runs daily to ftp files from a site for analysis. It has to write to the database, so it has access to all the models I've defined, but it never needs to present anything to the user since it is invisible to them.

Resources