Coder Social home page Coder Social logo

rspec_testing_app's Introduction

RSpec Testing App

Challenges

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
  1. Create and run a migration to add a first_name and a last_name column to the users table.

  2. Write a full_name instance method in the User model to pass the test you added.

  3. 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.

  1. Write a generate_username method in the User model that passes your tests.

Controller Tests with Recipes!

  1. 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.

  2. 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.

  3. Update your controller to fill in the index action, and make sure it passes the tests you wrote.

  4. 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.

  5. Update your controller to fill in the new action, and pass the tests in your spec.

  6. 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?

  7. 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 a show action for this step!

  8. 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 in rake routes). Hint: How will you get the id to use in the test?

  9. Update your controller to pass the tests you wrote for your show action.

  10. At this point, make sure all the tests you wrote for your create action are passing as well.

Stretch Challenges

  1. Write a spec for an edit action. It should render the edit view (which shows the edit recipe form). The edit action is associated with a parameterized url. Since we want to display the current version of the recipe within the form, the edit action will use the id from the url to get that information from the database and make it available in the edit view.

  2. Update your controller to pass the tests you wrote for your edit action.

  3. 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.

  4. Update your controller to pass the tests you wrote for your update action.

  5. 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.

  6. Update your recipe controller spec to check that when a user creates a recipe, it's saved to the user's recipe list.

  7. Refactor the create method in your RecipesController to pass your new create action tests.

rspec_testing_app's People

Contributors

ben-manning avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.