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. Writing a single line of Puppet code.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. to raise_error ( Puppet :: Error, /compress must be true or false/ ) end end endĬongratulations, you’ve just written a set of tests for a defined type without ( modules/logrotate/spec/defines/rule_spec.rb) require 'spec_helper' describe 'logrotate::rule' do endĪs this is a defined type, the first thing we need to do is give it a title A file resource that drops your logrotate rule into /etc/logrotate.dįirst off, lets create a skeleton spec file for your defined type.Includes the logrotate::setup class which handles installing logrotate.Lets say you’re writing tests for a logrotate::rule type that does two If you are notįamiliar with the basics of RSpec, I highly recommend you take some time before Use the extended functionality that rspec-puppet provides. This is not intended to be an RSpec tutorial, just an explanation of how to Tests should go in spec/defines, host tests should be placed in spec/hostsĪnd function tests should go in spec/functions. Class tests should be placed in spec/classes, defined type Testįiles should always end in _spec.rb (generally, they’re named _spec.rb). The important thing is what you name your test file and where you put it. require 'spec_helper' describe '' do # Your tests go in here end Whether you’re testing classes, defined types, hosts or functions the structure You would test for the existence of the apache::vhost types, but not for theįile resources created by them, that’s the job of the tests forĪpache::vhost. By this I mean, when testing your ‘webserver’ role class, When writing your test cases, you should only test the first level of resources With a path of /etc/apache2/sites-available/foo when run on a Debian host. Want to test that your apache::vhost defined type creates a file resource Your manifests into a catalogue of Puppet resources. Rspec-puppet tests are there to test the behaviour of Puppet when it compiles Test the result of the manifest on a live system. There are a lot of people confused by the purpose of these tests as they can’t When passed parameters, these tests become invaluable when adding newįunctionality to your modules, protecting against regressions when refactoring However, when you start leveling up your modules to include dynamic contentįrom templates, support multiple operating systems or take different actions Than simply duplicating your manifests in a different language and, for basic At first glance, writing tests for your Puppet modules appears to be no more
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |