Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RSpec without Rails?

Tags:

tdd

ruby

rspec

bdd

What is the process for doing TDD in Ruby with RSpec without Rails?

Do I need a Gemfile? Does it only need rspec in it?

Ruby 1.9.3

like image 202
B Seven Avatar asked Sep 06 '12 19:09

B Seven


People also ask

How do I set up RSpec?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.

What is RSpec used for?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What RSpec method is used to create an example?

The describe Keyword The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.

Is RSpec open source?

Owl - Open Source RSpec Test Reporting Tool. Owl is an open source tool used for the reporting and presentation of tests results achieved using the RSpec open source testing tool. It stores the test results in a PostgreSQL database and provides features to report them.


2 Answers

The process is as follows:

Install the rspec gem from the console:

gem install rspec 

Then create a folder (we'll name it root) with the following content:

root/my_model.rb

root/spec/my_model_spec.rb

#my_model.rb class MyModel   def the_truth     true   end end  #spec/my_model_spec.rb  require_relative '../my_model'  describe MyModel do   it "should be true" do     MyModel.new.the_truth.should be_true   end end 

Then in the console run

rspec spec/my_model_spec.rb 

voila!

like image 105
Erez Rabih Avatar answered Oct 09 '22 03:10

Erez Rabih


From within your projects directory...

gem install rspec rspec --init 

then write specs in the spec dir created and run them via

rspec 'path to spec' # or just rspec to run them all 
like image 31
Kyle Avatar answered Oct 09 '22 02:10

Kyle