andrunix
andrunix

Reputation: 1754

How to test simple Ruby class in Rails application?

I have a Rails 4 application to which I am adding a simple class as a wrapper around an external API I am calling. The class does not inherit from anything and looks something like:

class MyFacade
  def self.foo
    WebService::foo
  end

  def self.bar
    WebService::bar
  end
end

I have placed the file into app/classes where it gets autoloaded. This is working fine. The question is, how and where do you write tests for a class like this with only class methods?

Upvotes: 1

Views: 51

Answers (2)

Mike Manfrin
Mike Manfrin

Reputation: 2762

Exactly the same way you'd test an ActiveRecord model.

Your tests would look something like:

expect(MyFacade.foo).to be_kind_of(WebService::foo)

etc.

Upvotes: 2

blowmage
blowmage

Reputation: 8984

I would create the file test/models/my_facade_test.rb and test it there. This is still part of your domain, even if it doesn't inherit from ActiveRecord. Not every domain model needs to be backed by a database table.

Upvotes: 2

Related Questions