igor_rb
igor_rb

Reputation: 1891

Rspec describe method syntax

I don't find syntax of Rspec describe method, but find some examples. If I correctly understand, we can pass into describe method a string, a class name (model name, for example), and a string and class name together as a parameters. What difference between these three cases of invocation describe?

describe 'string' do
...
end

describe ModelName do
...
end

describe 'string', ModelName do
...
end

Upvotes: 0

Views: 2471

Answers (2)

Neil Slater
Neil Slater

Reputation: 27207

It is not a heavily used feature (admittedly in my limited experience), but describe can feed subject if provided with a module or class name (or presumably other objects under test)

class Foo
end

describe Foo do
  it "should be a Foo" do
    subject.should be_a Foo
  end
end

In the above example, which passes, giving describe a class name has caused it to return Foo.new from subject. Whereas passing the string "Foo" would not work the same way.


Another example:

describe  [], "an empty array" do
  it "should return nil from any index" do
    subject[1].should be_nil
  end
end

Running it:

$ rspec -f d rspec_describe.rb

[] an empty array
  should return nil from any index

Finished in 0.00255 seconds
1 example, 0 failures

Upvotes: 4

ck3g
ck3g

Reputation: 5929

It depends on thing you want to describe.

That description is for you and other developers working with that code base.

$ rspec --format=documentation spec/

or just

$ rspec -fd spec/

will out

string
  ...
ModelName
  ...
string ModelName
  ...

Upvotes: 2

Related Questions