boddhisattva
boddhisattva

Reputation: 7380

How do we test a Rails Model which does not have an equivalent table in the backend using RSpec

I have a Moderator model which basically queries web site related stat results from other models.

An e.g. of displayed stats could be the total number of users belonging to a particular area out of many areas in a city. Limiting the number of such records to a fixed number. For this, the body defined within the Moderator model makes use of an Area model.

Since the queries are not using anything from the same model, but actually from other models, there wasn't a need for me to have a table migration wrt this model.

I basically am now trying to test the defined methods in the Moderator model using Rspec. What I am basically trying to test is that a call to the method should return success, this I am doing through:-

subject.send(:method_name)
response.should be_success

I'm getting a common error for all such defined methods saying that database_name.table_name does not exist. Well , this is true but how should is it really making a difference?, and how to get a work around for this to just test a simple use case of calling a method successfully in the above context.

Thanks.

Upvotes: 1

Views: 45

Answers (2)

sorens
sorens

Reputation: 5060

Try something like this

describe Moderator do
  it "should do something" do
    moderator = Moderator.new
    moderator.something.should == "do"
  end
end

This assumes that you have a method something on Moderator and that it returns the string "do".

I think the RSpec Book provides great information and examples if you want to get more in-depth with your rspec.

Upvotes: 1

boddhisattva
boddhisattva

Reputation: 7380

Well,

The below line code did work for me:-

Model_name.method_name.should_not be_nil

Upvotes: 0

Related Questions