01-02-2022
Meta Stuff
config, updates, installs, software, etc
Today's Foci
Focus 1
- Submit bud forms
- they will be sumbitted - DONE
Focus 2
- Later, do the Prevent resource also
- It will be completed
Focus 3 continue with task
- Make steady progress on the birthday challenge using Sinatra and TDD with Capybara?
- Make some measurable progress
Birthday challenge
How do I TDD this?
I'm assuming I can use capybara Selenium to drive the browser - but how do I test the return values - honestly I can't even remember if there were return values from capybara - gah!
Sinatra + Capybara TDD resource @learnhowtoprogram
Hopefully the above helps. I'm beginning to think I should have worked on the prior days summaries / goals, but I suppose I can find time for that later.
ran git init ofc.
guide suggests a Gemfile which sounds standard - set the 3rd party manifest.
source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'rspec'
gem 'capybara'
gem 'pry'
Looks like what we were using yesterday, so should be good. Time for bundle!
bundle has installe 20 gems; I then ran rspec --init. ace.
Hmmm. I need to understand whats happening at the above link.
requires the app, and capybara/rspec
Capybara.app = Sinatra::Application
Well that seems to associate the Capybara with the Sinatra App - I don't understand what the '::' do in Ruby, however.
Tangent - '::' Namespace Operator
:: is an unary namespace resolution operator
module SomeModule
module InnerModule
class MyClass
CONSTANT = 4
end
end
end
source above!
If I wanted to access the constant i would do something like
SomeModule::InnerModule::MyClass::CONSTANT
which would evaluate to 4
If i wanted to return to the root
::SomeModule
would do the trick.
Birthday Challenge cont.
Okay i don't understand the describe statement here. Previously wew used a Class name.
So.
This has a string, and then a hash containing two symbols. I'd imagine these exist somewhere in the Capybara docs. I'll have a look.
What am I searching for?
Capybara / RSpec / Sinatra
Set an Rspec file, which is testing the webpage and the route.
I'm stuck. This requires working knowledge of Capybara, Sinatra, RSpec, Ruby. I don't feel I have this, nor do I feel this is attainable in the alloted time. That's a shame.
Onward.
Onward without testing. A little frustrating - this feels like Flask - and I'd rather spend my time learning how to implement TDD on the web, via Selenium. But I need to move on.o
I can't. I've started the tests. I can do this.
This is winging it - it's a very simple app, but I have no User Stories nor UML. There's a lot of winging.
I have no idea if the below is right.
require 'capybara/rspec'
require './app'
Capybara.app = Sinatra::Application
describe( 'birthday', {:type => :feature} ) do
before(:each) do
visit('/')
end
it 'fills in name' do
fill_in 'your name', with: 'Frank'
find_field('your name').should have_content('Frank')
end
end
so now I need an HTML form which called 'your name' - and RSpec should use this form via Capybara DSL, to use Selenium, and interact with the webpage.
Okay.
Let's make this happen.
huh - i started the webpage, but forgot to run the tests.
They're red - which doesn't mean it's working 100% correctly - but it seems so be looking on a local webpage at '/' for a 'your name' form.
That's a success in my book!
Now - whats the syntax for linking a Sinatra function to an HTML file?
and how do HTML forms work?... haha!o
get '/' do
erb :index
end
says something like the above?
which should link to a file
./views/index
File and filepath made.
Emmet is a godsend
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>
Now to link them - I've made my view - I'm guessing Sinatra is the Controller in this scenario? And no models yet, obviously.
Well I was wrong about
erb :index
but what does work is
get '/' do
send_file 'path/target.extension'
end
wonderful - i wonder what RSpec does now?
nothing much different to be honest!
so now let's put a form in to accept a name. The form must be called 'your name'
Some experimentation has happened. I've had to change a few things. But...
./app.rb
require 'sinatra'
require 'sinatra/reloader' if development?
get '/' do
send_file './views/index.html'
end
./spec/birthday_spec.rb
require 'capybara/rspec'
require './app'
Capybara.app = Sinatra::Application
describe( 'app', {:type => :feature} ) do
before(:each) do
visit('/')
end
it 'fills in name' do
fill_in 'yourname', with: 'Frank'
expect(page).to have_field('yourname', with: 'Frank')
end
end
./views/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Birthday App</title>
</head>
<body>
<h1>Birthday App</h1>
<h3>Input your Name</h3>
<form>
<label for="yourname">What is your name?</label><br>
<input type="text" id="yourname" name="yourname"><br>
</form>
</body>
</html>
It ain't pretty, but it's my first ever TDD web application! And I managed it alone! Huzzah!
What would I do differently? Ugh, difficult to say. The problem is my shallow understanding of RSpec, Ruby, Sinatra, Capybara, and Selenium.
Is there a way around those?
That was great - I didn't think I'd be able to do that. But it's breaktime. I need to get up for a bit. Maybe I'll just take my lua few minutes early, then continue from lunch end - 1400? I think that's a good idea actually.
I'm back! What to do? continue with this I suppose.
What do I need to add next?
Day and Month labels - with options I need to look at the form documentation again - end reset the before (:each) '/' statement in the spec file
right, I can do a form that takes only days of the month
ugh.
right I need something like
{ month: days }
{ January: 31 }
or something for the form
and then check the number is between 1 and the hask key, where the hash value is also on the form.
month_hash = { month: days }
month_hash.each do
form
Your birthday is
month_hash.key, month_hash.value
and then send that into the app.
oof. Right. Sinatra templating. lets go.
Riiiight. 'erb' the problem I encountered above, is the Embedded Ruby templating. Time for some modification.
Easy as pie. Wunderbar.
Now lets send that hash in... :)
I think it's sent in?
This is taking too long.
I should just build the thing.
That's the individual session almost over.
I'm glad I could test drive a web app using unfamiliar tools. Feels like a step in the right direction.
What went well?
Finding others' solutions and breaking them down step by step helped me understand the why behind the syntax - then if need be, I can find updates help or use the docs, to a limited extent due to my inexperience with these tools, and reutilise my understanding, instead of reutilising the syntax.
Why?
Because I'm spending most of my time looking for evidence and understanding it. I can reapply understanding. I can't reapply outdated syntax, or tools.
What could have been improved?
I feel little progress was made. I don't understand the tools enough to utilise them effectively.
How can I act on this?
I want to move on through the course. I suppose, again, return to speeding through it and returning later. But that doesn't work either, due to the amount of content to get through - it is designed to overwhelm, and even prevent competion - which is understandable, to be honest.
I feel conflicted - I went too deep and perhaps should have just made the thing function. I got a lot out of exploring, but I have built this sort of web app before in other languages. Perhaps I should have skipped this specific task altogether.
Pairing Challenge
passing in raw ruby must be instance variables
@something = params[:url]
POST to modify a server resource
url is a get request
We made good headway on the Web app. Went a little off the rails - but nonetheless I feel progress was made - completely forgot it was a guided explanation and jumped straight in as a team. Decisions were made, classes were made, tests were passed; that's a win in my book.
I even managed to setup my remote address without causing problems this time!