chrisma / i22 Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
When creating a paper without venue
Then validation should fail
Papers should not be valid without having all attributes (title, venue and year) set. Year should be a numeric value.
Expected #<paper id: nil, title: "computing machinery and intelligence", venue: nil, year: 1950, created_at: nil, updated_at: nil> not to be valid
24 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper
When a user visits the paper's edit page
Then it should have a single multiple select box
Make sure the n:m relation of papers and authors works in your automated tests, i.e. models are annotated with has_and_belongs_to_many
and a db migration for the new join table was created and run.
On a paper's edit page, it should be possible to add multiple authors.
An HTML multiple select element allows this.
Rails' select helper in combination with the 'options_from_collection_for_select' helper can be used to create the required HTML element:
<%= form.select 'author_ids',
options_from_collection_for_select(Author.all, :id, :name, paper.author_ids),
{},
multiple: true, size: 10 %>
Notice the usage of author_ids
in the code snippet. These are the ids transmitted by the form to the server.
The paper controller must be able to deal with the additional author information that is passed by the new select element to save the changes.
Make sure that you have "permitted" the parameters for passing the author ids to the paper controller (function paper_params
) using :author_ids => []
.
Expected to find css "select[multiple]" 1 time but there were no matches
40 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When a user visits the new author page
Then it should have a text input field with the name 'author[first_name]'
The "new author" page (at /authors/new
) is empty. Before adding input fields, create a test that checks for their presence.
In the spec file (spec/features/author/new_author_spec.rb
), create a new it
block with a descriptive name. Inside the block, after visiting the "new author" page, the test should check for input fields.
RSpec uses matchers to check if a given object is present or valid. For example have_field
checks if the HTML of the page contains an input
form field with the given label, name or id. For more matchers, see this Cheat Sheet or the documentation.
The following code snippet shows what your test could look like. You can copy&paste this code, but understand what every instruction does before moving on.
it "should have text inputs for an author's first name, last name, and homepage" do
visit new_author_path
# these are the standard names given to inputs by the Rails form builder
expect(page).to have_field('author[first_name]')
expect(page).to have_field('author[last_name]')
expect(page).to have_field('author[homepage]')
end
After committing the test, add the form fields for first_name
, last_name
and homepage
. It should look something like this. To create a form within the views/authors/new.html.erb
view, you can use a form builder, which generates the required HTML.
The primary form builder for Rails is provided by a helper method called form_with
. See the Rails Guide.
Don't worry if submitting the form causes an error at this point, that functionality will follow.
Expected to find field "author[first_name]" that is not disabled but there were no matches
2 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given an author named 'Alan Turing'
When a user visits Turing's details page
Then no error should occour
A 'show' page displays an author's details.
In a test, visit the show page of @alan
with visit author_path(@alan)
.
Create a separate file for author show page feature tests. Make sure it ends with _spec.rb
, so that RSpec recognizes it as a test and runs it.
Test for existence of text on a page using the have_text
matcher:
expect(page).to have_text(...)
.
Create a show
action on the AuthorsController and a 'show' view (app/views/authors/show.html.erb
).
Here is the relevant section (5.7) of the Rails Guide.
Tip: FactoryBot.create :author
creates and saves a valid author object in your tests. If you un-comment the code in spec/factories/authors.rb
, the author will be initialized with meaningful values.
Got AbstractController::ActionNotFound: The action 'show' could not be found for AuthorsController
7 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given an author named 'Alan Turing'
When a user visits the edit page of the author Alan Turing
Then no error should occour
Feature: Changes made to the authors in an edit form (similar to the 'new' form) should be written to the database.
New controller actions (edit and update) and a new view are required.
Tests could be placed in spec/features/author/edit_author_spec.rb
.
In a test, if an object's values should be reloaded from the database, after an action has modified them, consider the reload
method:
@author.reload
Here is the relevant section (5.11) of the Rails Guide.
Got AbstractController::ActionNotFound: The action 'edit' could not be found for AuthorsController
16 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given an eager student
When they have completed the Ruby on Rails exercise
Then they should celebrate!
Thanks for taking part in the exercise! We would appreciate your feedback to continue to improve in the future.
Please fill out this survey to help the teaching team improve this exercise
Got Students: 'Done!'
43 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper
When users visit the papers index page
Then it should show a a delete link for every paper
Expected to find css "a[data-turbo-method='delete'][href='/papers/1']" but there were no matches
36 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When a user visits the new paper page
Then the page should render
A paper management system manages papers in addition to authors.
Papers should have a 'title' and a 'venue' string as well as a 'year' integer, indicating publishing date.
Papers should be manageable by users using views similar to authors.
Instead of generating the paper model and all the needed controllers and views separately (for new, create, read, update, etc.), use a Rails scaffold
.
It generates a model, a database migration, controller and views, and a basic suite:
rails generate scaffold paper title:string venue:string year:integer
Take a look at the generated files and adapt the tests.
The new migrations need to be applied: rails db:migrate RAILS_ENV=development && rails db:migrate RAILS_ENV=test
Got NameError: undefined local variable or method `new_paper_path' for #RSpec::ExampleGroups::NewPaperPage:0x00007fe8509fe7d8
Did you mean? new_author_path
19 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When creating a paper without year
Then validation should fail
Papers should not be valid without having all attributes (title, venue and year) set. Year should be a numeric value.
Expected #<paper id: nil, title: "computing machinery and intelligence", venue: "mind 49: 433-460", year: nil, created_at: nil, updated_at: nil> not to be valid
26 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper
When users visit the papers index page
Then it should link to the paper's edit page
Expected to find link nil with href "/papers/1/edit" but there were no matches
35 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper with one author
And another author called 'Peter Plagiarist'
When a user visits the paper's edit page
And then selects 'Peter Plagiarist' as author
And submits the form
Then 'Peter Plagiarist' and 'Alan Turing' should be authors of the paper
The paper controller must be able to deal with the additional author information that is passed by the new select element to save the changes.
Make sure that you have "permitted" the parameters for passing the author ids to the paper controller (function paper_params
) using :author_ids => []
.
Expected collection contained: [#<author id: 1, first_name: "alan", last_name: "turing", homepage: "http://wikipedia.de/alan_turing"...eated_at: "2022-10-28 10:58:07.166196000 +0000", updated_at: "2022-10-28 10:58:07.166196000 +0000">]
actual collection contained: [#<author id: 1, first_name: "alan", last_name: "turing", homepage: "http://wikipedia.de/alan_turing"...eated_at: "2022-10-28 10:58:07.164877000 +0000", updated_at: "2022-10-28 10:58:07.164877000 +0000">]
the missing elements were: [#<author id: 2, first_name: "peter", last_name: "plagiarist", homepage: "http://wikipedia.de/alan_tu...eated_at: "2022-10-28 10:58:07.166196000 +0000", updated_at: "2022-10-28 10:58:07.166196000 +0000">]
41 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When a user visits the authors index page
Then the page should render
Feature: an overview page of all saved authors (index
action on AuthorsController), new view (app/views/authors/index.html.erb
).
Create a separate file (ending in _spec.rb
) for feature tests for this page.
The overview page should contain an HTML table with the headings "Name" and "Homepage", listing the full names as well as homepages of authors and should link to individual author's detail pages. The page should also contain a link to add new authors.
The have_link
method might be helpful to test the existence of links:
expect(page).to have_link 'New', href: new_author_path
Here is the relevant section (5.8) of the Rails Guide.
Got AbstractController::ActionNotFound: The action 'index' could not be found for AuthorsController
8 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When creating a paper with year 'nineteen-fifty'
Then validation should fail
Papers should not be valid without having all attributes (title, venue and year) set. Year should be a numeric value.
Expected #<paper id: nil, title: "computing machinery and intelligence", venue: "mind 49: 433-460", year: 0, created_at: nil, updated_at: nil> not to be valid
27 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper
Then it should have an empty list of authors
A paper can have many authors and an author can write many papers.
This relationship is indicated in Rails by annotating both models (i.e. the Author
and Paper
) with a has_and_belongs_to_many
association.
To store this n:m relation, create a new database table in a migration: guide.
There is a generator which can produce join tables if 'JoinTable' is part of the name:
rails generate migration CreateJoinTableAuthorPaper author paper
To test if such a relation exists, you can test whether a paper has an empty list of authors.
Reminder: Migrations are stored in db/migrate
and are applied with the rails db:migrate
command.
Got NoMethodError: undefined method `authors' for #Paper:0x0000562168a73840
37 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper with an author
When a user visits the paper's show page
Then it should show the author's full name
Expected to find text "alan turing" in "title: computing machinery and intelligence\nvenue: mind 49: 433-460\nyear: 1950\nedit this paper | back to papers | delete this paper"
39 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When creating an author without last name
Then validation should fail
Author data should pass checks, i.e. validations in Ruby on Rails, before being saved.
In particular, an author's last name should not be an empty string.
In your test for this behavior, be_valid
can be used to test if a model instance passes validations:
expect(@author).to_not be_valid
Here is the relevant section (5.10) of the Rails Guide.
Expected #<author id: nil, first_name: "alan", last_name: nil, homepage: "http://example.com", created_at: nil, updated_at: nil> not to be valid
13 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When an author is created with first name 'Alan', last name 'Turing', and homepage 'http://wikipedia.org/Alan_Turing'
Then an instance of Author should be created
The author model should have a first_name
, last_name
and homepage
, which are all strings.
It should furthermore have a method name
(no parameters) which returns the full name of an author (the first name followed by the last name), e.g. 'Alan Turing'.
Test this model behavior in a model test. Create spec/models/author_spec.rb
and in the describe
block, specify type: :model
.
In a model test, behavior is tested directly on the model object, e.g.
author = Author.new(...)
expect(author.first_name).to eq(...)
The eq
matcher tests for object equivalence.
See this reference for more matchers.
To pass your test a Rails model for authors must be created. Rails has generators
which make creating things easier. You can answer 'no' when Rails asks if you want to overwrite files, then your edited files stay the way they are.
Here is the relevant section (5.4) of the Rails Guide. The generator creates the required model attributes (e.g. first_name:string
, see db/schema.rb
for the changes) and maps them to the corresponding model. You can then use the attributes as variables in the model (i.e. author.first_name
).
Additional model methods are specified in the model's file, e.g. app/models/author.rb
using standard Ruby syntax.
Got NameError: uninitialized constant Author
4 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given an author named 'Alan Turing'
When users visit the authors index page
Then it should have a link to the individual author's edit page
The table on the author overview page should include additional links to the edit form of each listed author.
Adapt index_author_spec.rb
and the author index view.
Expected to find link nil with href "/authors/1/edit" but there were no matches
17 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given a paper published in 1950
And a paper published in 1968
When users visit the papers index page with url parameter year=1950
Then it should not show the paper published in 1968
Almost done with the exercise!
A long list of papers can be hard to search. Allow filtering the list of papers on the paper index page (by year) with an URL parameter, e.g. http://localhost:3000/papers?year=1970
.
You can use scopes to implement this behavior in the Paper
model and adapt the PapersController
's index action to take params[:year]
into account.
Consider the subheadings Passing in arguments and using conditionals of the Rails Guide.
Expected not to find text "go to statement considered harmful" in "papers\ntitle: computing machinery and intelligence\nvenue: mind 49: 433-460\nyear: 1950\nauthors:\nshow | edit | delete\ntitle: go to statement considered harmful\nvenue: communications of the acm\nyear: 1968\nauthors:\nshow | edit | delete\nnew paper"
42 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When a user visits the new author page
Then the page should be available at the new_author_path
route and should render without errors
Currently, the application contains a single route (at /
, see config/routes.rb
), a view (app/views/home/index.html.erb
) and a controller (app/controllers/home_controller.rb
), connecting them.
All available application routes are displayed using the command rails routes
.
Before implementing the requested feature, write a test for it. The file spec/features/author/new_author_spec.rb
contains the needed test case, it needs to be un-commented.
The describe
block defines which aspect will be tested. Specifying the feature
type includes helper methods for simulating browser actions in tests. Each it
block is a single test case. The test tries to access the path (i.e. the URL) of the "new author" page. This fails because no such path is defined yet.
Run the test (bundle exec rspec
) and commit the failing test case.
To make the test pass, see the Rails Guide for creating the appropriate controllers, views and setting up routes for authors. Note: Instead of articles (in the guide), this exercise is concerned with authors.
You can learn more about RSpec, the testing framework here.
Got NameError: undefined local variable or method `new_author_path' for #RSpec::ExampleGroups::NewAuthorPage:0x00007f90742d4040
Did you mean? new_polymorphic_path
1 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given an author named 'Alan Turing'
When users visit the authors index page
Then it should have link to delete an author
To delete a record following the REST paradigm, a DELETE request needs to be made.
As not many browsers support this directly, the DELETE call is made using JavaScript.
Newer versions of Rails support this using the turbo
dependency.
Install it using rails importmap:install turbo:install stimulus:install
(answer 'y' to overwrite existing files).
The following data
attributes (read by JS) turn the usual GET request of a link into a DELETE (with confirmation messge):
<%= link_to 'Delete', author_path(author), data: {turbo_method: :delete, turbo_confirm: "Are you sure?"} %>
After an author is deleted Author.count
should have decreased.
A destroy
controller action is needed.
Here is the relevant section (5.13) of the Rails Guide. (Please be aware that the link_to
syntax of the guide is outdated, see code above.)
Expected to find css "a[data-turbo-method='delete'][href='/authors/1']" but there were no matches
18 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When a user visits the new author page
And fills in 'Alan', 'Turing', and 'http://wikipedia.org/Alan_Turing', respectively
And submits the form
Then Alan Turing should be found in the database
Write a test that visits the new-author page, fills in the 'new author' form and submits it.
Use fill_in
to enter data into input fields.
Submit the form using the find
and click
methods.
Try a new it
block in new_author_spec.rb
.
visit new_author_path
page.fill_in 'author[last_name]', with: 'Dijkstra'
# ...
find('input[type="submit"]').click
Here is the relevant section (5.6) in the Rails Guide.
Make the failing test pass by saving the POSTed form data in a new Author database object.
This is the task of a create
action in authors_controller.rb
.
Make sure to set the :url
option of the form_with
, to control where the data will be POSTed to. Read up on "strong parameters" and whitelisting of required parameters.
Do not issue a redirect_to @author
, as this will error (there is no code for showing authors yet). Instead you can redirect_to root_path, notice: 'Success!'
.
Tips:
rails console
allows you to interact with stored object in an interactive shell, e.g. Author.count
shows the amount of stored authors.Got ActiveRecord::RecordNotFound: Couldn't find Author with [WHERE "authors"."last_name" = ?]
6 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
Given users visit the new author page
And fills in only first name and homepage, and NOT the last name
When they click submit
Then the page should 'error' and what went wrong
Validation errors should be communicated to the user.
These are stored in the errors
attribute of model instances.
It can be checked (@author.errors.any?
) and can be iterated over to extract error details (@author.errors.full_messages.each
).
Adding this type of code also requires modifying the new
action of the AuthorsController.
Your test can try to create an invalid object (on the new author page) and check whether 'error' is displayed.
Here is the relevant section (5.10) of the Rails Guide.
Expected to find text matching /error/i in "success!\npaper management system\nyou are seeing the rendered view of 'views/home/index.html.erb'"
15 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
When creating a paper without title
Then validation should fail
Papers should not be valid without having all attributes (title, venue and year) set. Year should be a numeric value.
Expected #<paper id: nil, title: nil, venue: "mind 49: 433-460", year: 1950, created_at: nil, updated_at: nil> not to be valid
22 exercise tests have passed. There are 44 in total. You will solve multiple at once towards the end.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.