AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Rspec tutorial2/14/2024 Just as a sanity check, let’s start the Rails server and open up our new page in the browser to make sure it works as expected. Let’s modify the action’s template so it just says “Hello, world!”. Let’s generate a controller, HelloController, with just a single action, index. Now that we’ve gotten the “plumbing” work out of the way, let’s write some actual application code. After we run this command we’ll have a spec directory in our application containing a couple config files. Just like the Devise gem, for example, which requires us not only to add devise to our Gemfile but to also run rails g devise:install, RSpec installation is a two-step process. Install RSpecĪlthough we’ve already installed the RSpec gem, we haven’t installed RSpec into our application. # The following gems aids with the nuts and boltsĭon’t forget to bundle install. # Capybara, the library that allows us to interact with the browser using Ruby At the end of the exercise we’ll take a step back and talk about which library enabled which step of what we just did. Even with the comments, it may not be abundantly clear at this moment what each gem is for. I’ve added a comment next to each gem or group of gems describing its role. Let’s add the following to our Gemfile under the :development, :test group. We’ll also include the webdrivers gem which is necessary in order for Capybara to interact with the browser. Each library will be included in our application in the form of a gem. This is the step where RSpec and Capybara will start to come into the picture. Let’s also create our application’s database at this point since we’ll have to do that eventually anyway. I want to use RSpec, so I want us to start with no MiniTest. If we had done rails new hello_world without the -T flag, we would have gotten a Rails application with a test directory containing MiniTest tests. Initializing the application Run the “rails new” commandįirst we’ll initialize this Rails app using the good old rails new command. Our goal right now is not deep understanding but to begin putting one foot in front of the other. For the purposes of this “hello world” exercise, that’s okay. You also may well not understand where the RSpec stops and the Capybara starts. ![]() You may find some of the RSpec or Capybara syntax confusing or indecipherable. The tools we’ll be usingįor this exercise we’ll be using RSpec, anecdotally the most popular of the Rails testing frameworks, and Capybara, a library that enables browser interaction (clicking buttons, filling out forms, etc.) using Ruby. The ins and outs of writing tests get complicated quick enough that I think it’s valuable to start with an example that’s almost absurdly simple. But the goal is not to write a realistic test but to provide you with a mental “Lego brick” that you can combine with other mental Lego bricks later. ![]() I would probably never write a test in real life that just verifies the content of a static page. The goal here is just to walk through the motions of writing a test. ![]()
0 Comments
Read More
Leave a Reply. |