Skip to content

Instantly share code, notes, and snippets.

@s-espinosa
Created October 30, 2016 14:54
Show Gist options
  • Save s-espinosa/cce846b78c3c3fd46dd0158216b26bfc to your computer and use it in GitHub Desktop.
Save s-espinosa/cce846b78c3c3fd46dd0158216b26bfc to your computer and use it in GitHub Desktop.

#Problem Solving

When you don’t know where to start

Write a test!

  • Writing a test will often help drive your decision making. If you later realize that some other piece of the application needs to be implemented first, it’s o.k. to skip the test (put an x in front of the it in RSpec, or write skip in the first line of an it block.
  • If you’re having trouble deciding which test to write, ask yourself if there are any relationships where one thing depends on the existence of another?
    • If so, do the thing that needs to be done first.
    • If not, just pick one! Maintain a bias towards action. Often in the process of working on one aspect of an application you will create a tool that makes some other easier. Alternatively, you will work on one part of an application and realize it’s more dependent on other functionality than you initially realized. This doesn’t mean that your time has been wasted. Even if you need to reassess the code that you’ve written up to this point, it’s likely that much of the code that you’ve written can still be used with some adjustments.

If you know that you have limited energy...

  • What will be the lightest lift? Do that!
  • Alternatively, what feels most familiar? Solving one problem has the benefit of creating additional tools that you can use to attack the remaining problems.

If you don’t know how you want something to work...

  • Ask yourself how it would work in your ideal world. For example, if you want to display some information in a view, ask yourself, what would be the easiest thing for me to have access to in this view (e.g. an array? a hash? a single return value?), and then write the code in the view as though that were true. Now that you have an idea of what you want you can go worry about how to make it work in your model and/or controller.

When I have a solution that doesn’t seem to be working

Check your assumptions:

  • Read the output from your test carefully. Is the error coming from the file and line that you expect? If not, a number of things may have happened:
    • You may have assumed that one piece of the puzzle was working that hasn’t actually been implemented,
    • You may have completely solved the problem at hand and inadvertently broken something else without realizing. Sometimes it’s easy to not notice that you’ve actually succeeded in your current task if you continue to see errors pop up without reading carefully.
  • Use your tools to see what is happening
    • Pry/Byebug: These tools can tell you the current values of variables at a given point in your test/application. Have you created the things you expected to create? Do they have the relationships you expected them to have? For example:
      • Is the id field in the instance that you created in your test filled in? If not, it’s likely that you aren't successfully writing to your database for some reason. Is it not passing all of your validations?
      • Is the foreign key field of the instance you created filled in? If not, you likely haven’t created a relationship between two instances that you thought you did.
      • Similarly, is calling an ActiveRecord method on your instance returning an empty array (for example is user.ideas returning an empty AR association?)? If so, it’s possible that you either haven’t created the relationship, or you didn’t successfully create the instances you thought you did. Remember, you can do this in a single step (in the previous example by calling something like user.ideas.create(…)).
      • Be sure to walk through different parts of your application.
        • Are you hitting the controller/action that you expect to be hitting? If not, is something off with your route? The redirect in your controller? The link that you’re clicking in your view?
        • In your controller, are you passing the values you expect to be? Do they have the values you expect?
        • In your test, are you successfully saving to your database in your setup? Visiting the appropriate page?
    • save_and_open_page: The launchy gem allows us to view the status of a page at a particular moment in our test.
      • Are the items you thought were displaying actually displaying?
      • If not, be sure to check the source code for the page (most browsers will allow you to either view page source, or use developer tools to inspect the elements on a page). Do you have empty html tags (e.g. <div></div> or <ul></ul>) where you expect to be seeing content? It’s possible that the code you’ve written to iterate through a collection is correct, but that collection is empty.
      • Double check the path for the view you’ve edited against the current path in the test. Do they match? Have you accidentally implemented functionality somewhere else in your application, redirected to the wrong place in your controller, or visited the wrong path in your test?
      • Are you displaying extra information that looks like code? It’s possible that you’ve included an = on one of your erb tags that should not have it.
      • Are you seeing # where you expected to see information? It’s possible that you’re returning instances to your view rather than attributes of those instances.

Other common errors

  • method: :delete isn’t successfully submitting a delete request. Have you edited your app/assets/javascripts/application.js file? Did you remove lines that said //= require jquery or //= require jquery_ujs? Put them back! Those aren’t just comments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment