![]() For example, if you pass -p “*.rb”, rspec will run all Ruby files, not just the ones that end in “_spec.rb”. Load and run spec files that match the pattern PATTERN. Successful Example results will display in green text, failures will print in red text.ĭisplays full error backtraces in rspec’s output.ĭisplays Ruby warnings in rspec’s output. ![]() This option directs rspec to write the test results to the output file FILE instead of to standard out.Įnables color in rspec’s output. See the section on Formatters for more details about output formats. This option allows you to specify different output formats. ![]() By default, rspec runs all specified spec files, no matter how many failures there are. With this option, rspec will stop running specs after the first Example fails. The following table shows some of the permutations that could be added in new Examples (it blocks)Īdds PATH to the load (require) path that rspec uses when looking for Ruby source files.Īdds a specific source file to be required in your spec. It would make sense to add more examples that tests various types of input strings with the has vowels method. We fixed a bug in the regular expression which has vowels method but our tests are far from complete. Now, save the changes you just made in string_analyizer.rb and run the rspec spec command again, you should now see output that looks like −įinished in 0.002 seconds (files took 0.11401 seconds to load)Ĭongratulations, the examples (tests) in your spec file are now passing. The bug is simple to fix, open up string_analyzer.rb Input should detect when a string contains vowelsĭo you see what just happened? Our spec failed because we have a bug in spec/string_analyzer_spec.rb:6 # StringAnalyzer With valid spec/string_analyzer_spec.rb:9:in `block (3 levels) in 'įinished in 0.015 seconds (files took 0.12201 seconds to load) Now, run rspec spec again, you should see output that looks like this −ġ) StringAnalyzer With valid input should detect when a string contains vowelsįailure/Error: expect(sa.has_vowels? test_string).to be true Ren spec\string_analyzer_test.rb string_analyzer_spec.rb You can do that easily by running this command − ![]() Rename string_analyzer_test.rb to string_analyzer_spec.rb. The reason that this happened is that, by default, rspec only runs files whose names end in “_spec.rb”. You should see this result −įinished in 0 seconds (files took 0.068 seconds to load) When you pass the name of a folder to rspec, it runs all of the spec files inside of the folder. Now we’re going to run our tests, run this command: rspec spec In your cmd.exe window, cd to the C:\rspec_tutorial folder and run this command: dir spec Save this in the same spec directory, giving it the name string_analyzer_test.rb. It "should detect when a string doesn't contain vowels" doĮxpect(sa.has_vowels? test_string).to be false It "should detect when a string contains vowels" doĮxpect(sa.has_vowels? test_string).to be true Here is the source for our spec file to test StringAnalyzer − If you followed the HelloWorld section, you created a folder called C:\rspec_tutorial\spec.ĭelete the hello_world.rb file if you have it and save the StringAnalyzer code above to a file called string_analyzer.rb in the C:\rspec_tutorial\spec folder. Here’s the implementation for StringAnalyzer − Our class has only one method has_vowels? which as its names suggests, returns true if a string contains vowels and false if it doesn’t. It’s a simple class that, you guessed it, analyzes strings. In this chapter, we will create a new Ruby class, save it in its own file and create a separate spec file to test this class.įirst, in our new class, it is called StringAnalyzer.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |