Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec - how can I check if a method exists?

My model has

def self.empty_building
  // stuff
end

How can I rspec for this existing?, have tried:

describe "empty_building" do
  subject { Building.new }
  it { should respond_to :empty_building }
end

but getting :

Failure/Error: it { should respond_to :empty_building }  
expected #<Building id: nil, district_id: nil, name: nil, 
direct: nil, created_at: nil, updated_at: nil> to respond to :empty_building
like image 692
Michael Durrant Avatar asked Jul 08 '12 01:07

Michael Durrant


People also ask

How do I know if a method was called RSpec?

You can check a method is called or not by using receive in rspec.

How do I run a test in RSpec?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

What is RSpec command?

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.

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.


1 Answers

You have a class method

self.empty_building

in your model.. but your subject is an instance of Building.

So either, it should be

def empty_building 

or it should be:

describe "empty_building" do
  it { Building.should respond_to :empty_building }
end
like image 85
Jesse Wolgamott Avatar answered Oct 04 '22 00:10

Jesse Wolgamott