how to run capybara sinatra - ruby

I was given a sample sinatra project with a hello world for capybara testing in akephalos. I understand the concept by looking at the code, but how do i run it? If I run rackup config.ru, and then go to :9292 I just see a hello world. Great, what is that telling me? How do I run the test? The project is bare bones, but below is a file called example_spec.rb. How can I see it fail, for example by looking for "Hi world" and watching it fail? Hope this is enough info. Thought I would check here before I ask the dude that supplied me with the test, thanks!
# describe and context blocks are optional but help organize things
describe 'the index page' do
include x
# :js => true is used to run the test in Firefox. Otherwise it runs headless
# and without JS support
it 'can view the index page', :js => true do
visit '/'
# check to see if the page has the following text (ignoring tags)
page.should have_content('Hello, world!')
# visit https://github.com/jnicklas/capybara to see a complete list of
# assertions
end

You need to set Capybara.app = <your Sinatra class>. Perhaps something like this:
setup do
Capybara.app = Main
end

bundle exec rspec spec, This means run "bundle exec rspec" on the "spec" directory

Related

Launching Sinatra at Terminal stays blank

I was trying to run Sinatra and Ruby in my MacBook, and all was working fine. Then, suddenly, I tried again and it just stays like this:
I can't access to localhost or anything. I don't know what to do. I've been researching for hours. Please, help me.
This is what my ruby code looks like:
require 'sinatra'
gets '/ejemplo1' do
puts 'Hello World'
end
Seems to be a typo. Should be get and not gets.
require 'sinatra'
get '/ejemplo1' do
puts 'Hello World'
end
Additional info:
gets in ruby is a way to get user input:
name = gets
puts "Your name is #{name}"
Like mentioned by #Norly Canarias you should use get for routing in sinatra. Moreover if you use puts statement in get block it will print only in terminal when you run your code not in webpage when you access localhost. Correct way to make it display in webpage is given below
require 'sinatra'
get '/ejemplo1' do
'Hello World'
end

Capybara Around Hook to test several envinroments

I'm writing some tests for a webpage that I'd like to run in several environments. The idea is that the test will run in one, then repeat in the next. The two environments are preview and uat.
I've written an Around hook to set the environment variables. Below:
Around do |scenario, block|
def test_envs
chosen_env = ENV['test_env'] || 'preview'
chosen_env.split(',').map(&:strip)
end
test_envs.each do |test_env|
$base_url = "https://#{test_env}.webpage.com"
end
block.call
end
I have then written a method to execute the navigation step:
def navigate_to(path)
visit $base_url + path
end
My Scenario step_definition is:
navigate_to '/login'
The tests will work in either environment, Preview by default or UAT if I set test_env=uat
However, I was aiming to set test_env=preview,uat and have them run consecutively in both environments.
Is there something obvious that I've missed here?
Thanks
If I'm understanding you correctly, it's the 'parallel' aspect that you're asking about.
Rspec can be used with parallel tests (the parallel_tests gem) but I wouldn't be so sure that calling something like 3.times { blk.call } in an around hook will run each block in parallel.
An alternative may be do so some metaprogramming with your example definitions, i.e.
test_envs.each do |env_name|
it "does something in #{env_name}" do
# do something with the specific environment
end
end
Now, I haven't actually used this gem and I don't know for sure it would work. I think the simplest solution may be to just write a wrapper script to call the tests
# run_tests.rb
environments = ENV["TEST_ENV"]&.split(",") || []\
filename = ENV["filename"]
environments.each do |env_name|
Thread.new do
system <<-SH
env TEST_ENV=#{env_name} bundle exec rspec #{filename}
SH
end
end
Running it like env TEST_ENV=foo,bar ruby run_tests.rb would call the following commands in their own threads:
env TEST_ENV=foo bundle exec rspec
env TEST_ENV=bar bundle exec rspec
I like this approach because it means you don't have to touch your existing test code.

Using guard-minitest on a single Ruby file

I'm clearly doing something wrong. I'm trying to write and test plain ruby in a single file. I want guard to watch the file and the test file and run minitest any time either file changes.
So, two files: game.rb and game_test.rb
game.rb
class Game
end
game_test.rb
require 'rubygems'
require 'minitest/autorun'
require './game'
class GameTest < MiniTest::Unit::TestCase
def test_truth
assert true
end
end
I also have a Guardfile that looks like this:
notification :terminal_notifier
guard 'minitest', test_folders: '.' do
watch('game.rb')
watch('game_test.rb')
end
Now, I'm probably forgetting something, but I can't for the life of me figure out what it is.
If I start guard and press Enter, "Run All" happens and the tests run.. at least most of the time. However, I have to press Enter for it to happen.
Also, if I make a change to the files nothing happens. I've tried putting gem 'rb-fsevent' in a Gemfile and running with "bundle exec guard" but that doesn't seem to help either.
Any help would be much appreciated. I'm going nuts.
Thanks,
Jeremy
Your first "watch" definition will simply pass "game.rb", which is not a test file so it won't be run.
The second "watch" is correct so when you save "game_test.rb", the tests should run.
This should be a more correct Guardfile:
notification :terminal_notifier
guard 'minitest', test_folders: '.' do
watch('game.rb') { 'game_test.rb' }
watch('game_test.rb')
end

very basic ruby/sinatra/heroku/debugging question: how to see output of puts and p?

I'm trying to build a very simple sinatra app deployed on heroku.
our app is not outputting stuff to a web browser, it's communicating with another computer via an API. so my usual trick of just printing a little extra debugging info to the browser while I'm using the app doesnt work.
the sample code I've seen for related apps show multiple 'puts' or 'p' statement used ot sort of see what's going on...
where does the output go that I can see that output as the program executes, or afterwards.
and in general, if you're flailing around with code hosted at Heroku that's just not doing what you want, what IS the easiest way to at various places in the code output messages like "foo equals 123" so you can see that output to figure out what's happening in the code?
p and puts dont output so the logs I can see when I type "heroku logs" for example...
If you use a cedar stack, try to put a line bellow in config.ru,
$stdout.sync = true
http://devcenter.heroku.com/articles/ruby#logging
Original post was in February 2011, and Cedar stack was introduced in May, so this doesn't seem to be help for original question, but some of you may find this could be help.
http://blog.heroku.com/archives/2011/5/31/celadon_cedar/
According to http://docs.heroku.com/logging you should be able to have puts and p just go to your log if you add the basic logger (which has apparently been added by default to all apps created after February 2nd, 2011).
For non-Heroku basic log-to-file with Sinatra and Logger:
require 'logger'
Dir.mkdir('logs') unless File.exist?('logs')
$log = Logger.new('logs/output.log','weekly')
configure :production do
$log.level = Logger::WARN
end
configure :development do
$log.level = Logger::DEBUG
end
get "/" do
$log.debug "Hello, World!"
end
This will work fine. test_logging.rb
require 'sinatra'
require 'logger'
enable :logging
before do
logger.level = Logger::DEBUG
end
get '/' do
logger.debug "Handling 'hello world' request."
logger.info "Hello world."
return "<h1>Hello World</h1>"
end
See here for tips on how to write to the Logger: http://mikenaberezny.com/2007/02/24/rails-logging-tips/
The example given is:
class HomeController < ActionController::Base
def index
logger.info 'informational message'
end
end
This is the line that works for me:
# On config.ru, before run:
enable :logging, :dump_errors, :raise_errors

How do you run a specific test with test/spec (not a specific file, but a spec within a given file)?

With Test::Unit, I can run:
ruby path/to/test.rb --name=test_name_that_i_want_to_run
Thus far, I have not been able to figure out how to do this with test/spec specifications. I am wondering if the way that specifications are automatically named does not allow me to do something like this.
Take the following spec for example:
require 'rubygems'
require 'spec'
describe 'tests' do
it 'should be true' do
1.should == 1
end
it 'should be false' do
1.should_not == 2
end
end
You can execute a single spec by using the -e flag and providing the portion specified by the it block. e.g. ruby my_spec.rb -e 'should be false'
After contacting the gem maintainer, Christian Neukirchen, I found out how to do this, so I am documenting it here for future reference.
specrb path/to/test.rb --name ".*should behave this way.*"
I needed to use the specrb test runner, an extended version Test::Unit's test runner, rather than just the ruby command.
You can also do this with the ruby command:
ruby path/to/test.rb -n "/should behave this way/"

Resources