We'll build off a rails app with auth. Fork and clone this app.
Model Method Tests
describe "#full_name" do
it "joins first name and last name" do
expect(@user.full_name).to eq("#{@user.first_name} #{@user.last_name}")
end
end
-
Create and run a migration to add a
first_name
and alast_name
column to theusers
table. -
Write a
full_name
instance method in theUser
model to pass the test you added. -
Write a spec for a
generate_username
method that combines the first letter of a user's first name with the user's full last name and a random 2 digit number (10 to 99). All letters should be changed to lower case. Examples:
# user1 has first_name "Cameron", last_name "Jacoby"
user1.generate_username
# => cjacoby64
# user2 has first_name "Erik", last_name "Erwitt"
user2.generate_username
# => eerwitt98
Hint: use a regular expression to check the format of the username.
- Write a
generate_username
method in theUser
model that passes your tests.
Controller Tests with Recipes!
-
Create a
Recipe
model and its controller. A recipe should include the dish's title and the instructions for making the dish. You can assume the instructions are plain text. -
Write the spec for
recipes#index
(recipes
controller,index
action). It should render an index view with data from all existing recipes in the database. Do you expect your tests to pass or fail? Run the spec. -
Update your controller to fill in the
index
action, and make sure it passes the tests you wrote. -
Write the spec for a
new
action. It should render a new view (which would have the new recipe form). Do you expect your tests to pass or fail? Run the tests. -
Update your controller to fill in the
new
action, and pass the tests in your spec. -
Write a spec for a
create
action. It should use data from parameters to add a recipe to the database, then redirect to a show view for the new recipe. Do you expect your tests to pass? -
Update your controller to fill in the
create
action, and pass as many of the tests you wrote as possible so far. Hint: Don't write ashow
action for this step! -
Write a spec for a
show
action. It should render a show view with information for a single recipe. It will be associated with a parameterized url (as you can see inrake routes
). Hint: How will you get theid
to use in the test? -
Update your controller to pass the tests you wrote for your
show
action. -
At this point, make sure all the tests you wrote for your
create
action are passing as well.
-
Write a spec for an
edit
action. It should render the edit view (which shows the edit recipe form). Theedit
action is associated with a parameterized url. Since we want to display the current version of the recipe within the form, theedit
action will use the id from the url to get that information from the database and make it available in the edit view. -
Update your controller to pass the tests you wrote for your
edit
action. -
Write a spec for an
update
action. It should take in new data for a specific recipe, change the recipe in the database, and redirect to the show page for the item. -
Update your controller to pass the tests you wrote for your
update
action. -
Let's give users the ability to create recipes associated with their own account. Set up a one-to-many (1:N) relationship between a user and their recipes.
-
Update your recipe controller spec to check that when a user creates a recipe, it's saved to the user's recipe list.
-
Refactor the
create
method in yourRecipesController
to pass your newcreate
action tests.