![]() ![]() The code expect(character).to be_an_instance_of Character almost reads like a real sentence! The idea here is that methods can be chained together in a way that sounds very close to plain English. If this line evaluates to True, our test will pass-otherwise, it will fail!Īs we saw above, RSpec provides an expressive domain-specific language (DSL) to write our tests with. This is where we write the test code logic! In a very expressive style, we see that the test itself expects the variable character to be an instance of the class Character. Finally, we have an it "description of expected behavior" do block.See this excellent comment from Andrew Brown to see why and learn the proper alternatives!) ( NOTE: This is NOT the right way to instantiate variables in an RSpec test! It is here only for an introductory example. This will give us all the objects we need to test the method's behavior. Inside that describe block for our method, we instantiate a couple objects to test, and run the new_character method so we can assign its output to the variable character.It is a Rails convention to add # before this-that way, the test output will read RandomCharacterGenerator#new_character, which helps us know what we're testing. This encapsulates our tests for the new_character method. Underneath, we have describe "#method_name" do.This encapsulates our tests for the RandomCharacterGenerator service object class. Starting a new Rails app with no test tool, and adding RSpecĮnter fullscreen mode Exit fullscreen mode The following instructions are pulled from the thread's answers. This is an excellent StackOverflow discussion for both starting a new Rails app with RSpec, and migrating an existing Rails app to RSpec (from test-unit, the default Rails test tool). ![]() When refactoring your code, make sure the tests still passĪdding RSpec to a new or existing Rails app Write tests for that class/method/behaviorĤ. Decide on the class/method/behavior to testĢ. refactor with the confidence that the tests will tell you if the behavior is not working anymoreĪpplying this to Rails (or Ruby more broadly, or object-oriented programming languages in general), these are the main steps we will use:ġ.write as little code as possible (to get the test to pass).If you know when your code is doing that behavior correctly, you can:.If you write tests for it, you will know when your code is doing that behavior correctly.If you know what behavior you need to see, you can write tests for it. ![]() The idea behind this is that being able to identify and articulate the behaviors you want to test should help guide you when writing the code itself: Essentially, the goal of both is: write tests first, then write code to make the tests pass! It's hard to understand RSpec without having some idea about behavior-driven development (BDD), so let's start there!īDD is a testing philosophy that grew out of test-driven development (TDD). ![]() I'm writing this as an intro to some basic patterns and syntax used in RSpec Rails testing, as well as some basic BDD strategy for breaking down code into testable units. RSpec is a behavior-driven development (BDD) testing tool for Ruby, and is widely used for testing both plain ol' Ruby and full-on Rails applications. As the class got more complicated, I realized that I had a very clear idea of the expected behavior- so this was a perfect opportunity to practice writing RSpec tests for those behaviors! While working on the character-generator API from my last post, I found myself writing most of my code in the RandomCharacterGenerator service object. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |