Question: Run basic rspec file

Question

Run basic rspec file

Answers 2
Added at 2017-01-05 14:01
Tags
Question

I'm new to Ruby and RSpec. I want to create very simple RSpec test:

# test_spec.rb
require 'spec_helper'

describe TestController  do 

    puts(Time.now)

end

But when I run the code this way rspec test_spec.rb I get error:

`<top (required)>': uninitialized constant TestController (NameError)

Can you give me some idea where I'm wrong?

Answers to

Run basic rspec file

nr: #1 dodano: 2017-01-05 15:01

If you pass a class to describe, RSpec attempts to create an instance of that class. If the specified class does not exist, you get an error.

You can pass a string instead:

describe "my first test" do
  it "does something" do
    expect(1).to be_odd
  end
end

Running the above:

$ rspec -fd test_spec.rb

my first test
  does something

Finished in 0.00178 seconds (files took 0.10381 seconds to load)
1 example, 0 failures
nr: #2 dodano: 2017-01-05 16:01

This error is because after the describe you need a string with the description and you are leaving it without quotes.

describe '3pController'  do 
    puts(Time.now)
end

But the tests should be structured as follows

  1. a (describe) can have many (it) inside (a (it) is where the tests are)
  2. one (decribe) can have more (describe) inside or can have (context), that these at the same time has many (it).

inside this link I will leave the documentation Basic structure

Here you can see a basic example of a test

describe 'sum 2 + 2 equal 4' do
   it { expect(2 + 2).to eq(4)}
end
Source Show
◀ Wstecz