Jim's
Tutorials

Fall 2018
course
site

testing week 13

So I wanted to start this weeks' testing explorations taking another look at how to use pry as I'm working with this server stuff. I used it once before but now would really like to see how I can monitor my 'inputs' etc and exactly where to inturrupt the server to get these answers.

I took a stab at inserting binding.pry in different places in my server, and got some confusing results.

example;

require "sinatra"
require "pry"
get "/items" do
  @items = ["00786", "88693", "66793", '77385']
  erb :index
end
get "/items/:item_name" do
  @item_name = params[:item_name]
  erb :show
end
binding.pry
post "/items" do
redirect "/items"
end

..so here I'm putting it after the second get. And I get these results once I start the server;

ruby server.rb
== Sinatra (v2.0.4) has taken the stage on 4567 for development with backup from Puma
Puma starting in single mode...
* Version 3.11.0 (ruby 2.3.3-p222), codename: Love Song
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:4567
Use Ctrl-C to stop
::1 - - [30/Nov/2018:12:25:12 -0500] "GET /items HTTP/1.1" 200 713 0.0176
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2018-11-30 12:25:26 -0500 ===
- Goodbye!
== Sinatra has ended his set (crowd applauds)
➜  todo_list git:(master) ✗ ruby server.rb

From: /Users/lesliewilson/marlboro_rails/todo_list/server.rb @ line 18 :

    13: get "/items/:item_name" do
    14:   @item_name = params[:item_name]
    15:   erb :show
    16: end
    17:
 => 18: binding.pry
    19:
    20: post "/items" do
    21: redirect "/items"
    22: end
    23:

Ok, fine. But when I go back to my form and try to submit something;

<!DOCTYPE html>
<html>
  <head>
    <title>Basic HTML Page</title>
    <link rel="stylesheet" href="home.css">
  </head>

  <body>
    <h1>index item list for server</h1>

    <ul>
        <% @items.each do |item| %>
          <li>
            <a href="/items/<%= item %>"><%= item %></a>
          </li>
        <% end %>
    </ul>

...the server crashes. So I'd like to figure out how to use pry exactly so I can interrupt this the right way.

OK figured it out- how to use pry and how to view what you submitted:

require "sinatra"
require "pry"
get "/items" do
  @items = ["00786", "88693", "66793", '77385']
  erb :index
end
get "/items/:item_name" do
  @item_name = params[:item_name]
  erb :show
end

post "/items" do
binding.pry
item = params["item_name"]
File.open("items.txt", "a") do |file|
    file.puts(item)
end
redirect "/items"
end

..So here I'm running through all the items and putting them in a file, I'm not exactly sure why I need to do that], does it create a kind of landing place for your submission that you can then intercept with pry?

anyway I pry'd the 'params' and got to see what I put in the form field.

From: /Users/lesliewilson/marlboro_rails/todo_list/server.rb @ line 15 self.POST /items:

    10:   @item_name = params[:item_name]
    11:   erb :show
    12: end
    13:
    14: post "/items" do
 => 15: binding.pry
    16: item = params["item_name"]
    17: File.open("items.txt", "a") do |file|
    18:     file.puts(item)
    19: end
    20:

[1] pry(#<Sinatra::Application>)> params
=> {"item_name"=>"9879"}

So thats cool.

Then I tried to make it so instead of whatever I submit, being saved to a text file, that it sort of just show up as a list on the browser. I tried putting each line in an array and tried debugging it like this:


From: /Users/lesliewilson/marlboro_rails/todo_list/server.rb @ line 10 self.GET /items:

     5: get "/items" do
     6:     @items = []
     7:     File.readlines("items.txt") do |line|
     8:         @items << line
     9:     end
 => 10:     binding.pry
    11:     erb :index
    12: end
    13:
    14: get "/items/:item_name" do
    15:   @item_name = params[:item_name]

[1] pry(#<Sinatra::Application>)> @items
=> []

my @items array kept coming up empty, it should have had everything in my text file (was like 7 entries) and actually I never got this to work using exactly this sytnax. But I posted in rails what I DID get to work.

Capybara acceptance testing

feature testing user_adds_numbers.rb

require 'spec_helper'
feature "User adds two numbers" do
    scenario 'supply two numbers and calculate the sum' do
        visit '/'
        fill_in 'First Number', with: '3'
        fill_in 'Second Number', with: '5'
        click_button '+'
        expect(page).to have_content("result: #{3+5}")
    end
end


server.rb

require 'sinatra'
require 'pry'

get '/' do
  erb :index
end

set :bind, '0.0.0.0'  # bind to all interfaces

post '/calculate' do
  @result = nil
  first_number = params[:first_number].to_i
  second_number = params[:second_number].to_i
  case params[:operation]
  when "+"
    @result = first_number + second_number
  when "-"
    @result = first_number - second_number
  when "*"
    @result = first_number * second_number
  when "/"
    @result = first_number / second_number
  else
    @result = nil
  end
  erb :index
end