Code Challenge and Learning Rspec
This week, I'm working on a code challenge that involves having to write tests.
While I've read Rspec testing, I have never written any...until now. I'm going to explain how to implement Rspec testing as I learn it.
First thing you want to do is make sure the rspec gem is installed. You can do that by typing gem install rspec into your terminal
Next, you want to make a spec folder in your project. Inside your spec folder, you will want to make the following: spec_helper.rb
For every class file you have, you will want to make a class_spec.rb file.
For example, if you have an Artist class, the spec file will be artist_spec.rb
So, your file tree should look something like this
artist.rb
>Spec
spec_helper.rb
artist_spec.rb
In your spec_helper.rb file you will want to do the following:
put a require_relative statement for each class.
Example: require_relative 'artist.rb' T
he other thing that you want to add is require 'yaml'.
Now you are ready to start writing tests. In your artist_spec.rb file, start off the file by doing the following: require 'spec_helper' at the top of the file Next you will want to start with your first describe block.
Inside your describe method for the class, you will want to specify an instance of the class and what it requires using a
before :each do block.
Here is where you can create your instance variable to test and specify how many arguments your class takes on initialization. After you complete that, you will want to create another describe block. In this next describe block, you are going to make sure that the class can be initialized
If you haven't written the beginning of your Artist class, you will get an error like this when you type rspec spec in the terminal.
NameError: uninitialized constant Artist # ./spec/artist_spec.rb:3:in `'
No examples found.
If you want to run tests for a specific class, you can type rspec and then the path to the tests.
Now you have the basics for testing done and can continue to write describe blocks that are nested within your describe block for the class.
Here are some additional resources that I found for writing tests.
https://relishapp.com/rspec/rspec-expectations/v/3-7/docs
Inside your describe block, you will want to use expect and then a way to compare. You can also expect something to not equal something as well. Take a look at the link for more ideas.
Hopefully this will help you get started with testing your code.
While I've read Rspec testing, I have never written any...until now. I'm going to explain how to implement Rspec testing as I learn it.
First thing you want to do is make sure the rspec gem is installed. You can do that by typing gem install rspec into your terminal
Next, you want to make a spec folder in your project. Inside your spec folder, you will want to make the following: spec_helper.rb
For every class file you have, you will want to make a class_spec.rb file.
For example, if you have an Artist class, the spec file will be artist_spec.rb
So, your file tree should look something like this
artist.rb
>Spec
spec_helper.rb
artist_spec.rb
In your spec_helper.rb file you will want to do the following:
put a require_relative statement for each class.
Example: require_relative 'artist.rb' T
he other thing that you want to add is require 'yaml'.
Now you are ready to start writing tests. In your artist_spec.rb file, start off the file by doing the following: require 'spec_helper' at the top of the file Next you will want to start with your first describe block.
Inside your describe method for the class, you will want to specify an instance of the class and what it requires using a
before :each do block.
Here is where you can create your instance variable to test and specify how many arguments your class takes on initialization. After you complete that, you will want to create another describe block. In this next describe block, you are going to make sure that the class can be initialized
If you haven't written the beginning of your Artist class, you will get an error like this when you type rspec spec in the terminal.
NameError: uninitialized constant Artist # ./spec/artist_spec.rb:3:in `
expect("this string").to start_with("this")
expect("this string").not_to start_with("that")
expect([0,1,2]).to start_with(0, 1)
Hopefully this will help you get started with testing your code.
Comments
Post a Comment