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

Posted by Mohnish J. (mohnish_j)
on 2011-11-21 12:28
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.
Posted by Sidu Ponnappa (kaiwren)
on 2011-11-21 14:13
(Received via mailing list)
> 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.
I don't believe this issue has anything to do with RSpec (you'd see
the same if you used MiniTest or any other tool).

You want to have Moderator be a simple Ruby object, or possibly have
it inherit from ActiveModel rather than ActiveRecord.

Best,
Sidu.
http://c42.in
http://rubymonk.com
Posted by Mohnish J. (mohnish_j)
on 2011-11-22 03:11
Sidu Ponnappa wrote in post #1032900:
>> 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.
> I don't believe this issue has anything to do with RSpec (you'd see
> the same if you used MiniTest or any other tool).
>
> You want to have Moderator be a simple Ruby object, or possibly have
> it inherit from ActiveModel rather than ActiveRecord.
>
> Best,
> Sidu.
> http://c42.in
> http://rubymonk.com

Thanks for your inputs Sidu,I tried replacing ActiveRecord with 
ActiveModel got the following error:-


/home/mohnish/.rvm/gems/ruby-1.9.2-p180/gems/restfulie-1.0.3/lib/restfulie/server/action_controller/patch.rb:2: 
warning: already initialized constant HTTP_METHODS
/home/mohnish/Cognizant/socialtango/app/models/admin_stats.rb:1:in `<top 
(required)>': uninitialized constant ActiveModel::Base (NameError)


Seems like, I will have to look for another workaround..
Posted by Mohnish J. (mohnish_j)
on 2011-11-22 17:10
Mohnish J. wrote in post #1033006:
> Sidu Ponnappa wrote in post #1032900:
>>> 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.
>> I don't believe this issue has anything to do with RSpec (you'd see
>> the same if you used MiniTest or any other tool).
>>
>> You want to have Moderator be a simple Ruby object, or possibly have
>> it inherit from ActiveModel rather than ActiveRecord.
>>
>> Best,
>> Sidu.
>> http://c42.in
>> http://rubymonk.com
>
> Thanks for your inputs Sidu,I tried replacing ActiveRecord with
> ActiveModel got the following error:-
>
>
> 
/home/mohnish/.rvm/gems/ruby-1.9.2-p180/gems/restfulie-1.0.3/lib/restfulie/server/action_controller/patch.rb:2:
> warning: already initialized constant HTTP_METHODS
> /home/mohnish/Cognizant/socialtango/app/models/admin_stats.rb:1:in `<top
> (required)>': uninitialized constant ActiveModel::Base (NameError)
>
>
> Seems like, I will have to look for another workaround..

Well,

The below line code did work for me:-

Model_name.method_name.should_not be_nil
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.