1

Guys bear with me on this. I have been asked by my management team to do something like below and I would highly appreciate any advise provided.

So lets say I have a controller with the below code:

class ListsController < ApplicationController
  def show
    @list = List.create(slug: params[:slug], something: params[:something])

    render 'home/index'
  end
end

Now I want to test this piece of code with rspec, but there is no db to save or receive an object( well there was one earlier but we have been asked to remove it when running tests).

So my question:

  1. First Is it at all possible to test this without a db?
  2. If yes then how?

So far I have tried using nulldb gem to remove the db dependency. But the problem arrives when the created object cannot be retrieved. I have tried stubbing and mocking the created object but it hasn't worked so far. Even if it does it would take ages to follow this and test the complete application.

Edit:

I have tried factory_bot as well. But that only helps instantiate an object. But how can I retrieve them later?

Let me know if there is anything you need to understand it better or if I have missed something. Guys any help appreciated.

7
  • You might want to look into this gem https://github.com/thoughtbot/factory_bot Commented Sep 4, 2018 at 22:37
  • let me know if this helps Commented Sep 4, 2018 at 23:21
  • While it can be done the current preferred way of testing controllers is through integration specs. Stubbing out the database in that case is a fools errand that will reduce the acuity of your tests and lead to bugs ending up in production. The problem might be explaining that to the management team. Commented Sep 5, 2018 at 0:12
  • @Verty00 I have tried factory_bot as well. They still need to be saved to be able to retrieve later. Their build works in some cases but there is no way to get something that can't be saved anywhere. Commented Sep 5, 2018 at 7:20
  • @max I totally agree with you. That is exactly what I have been trying to explain them as well but to no avail. They are adamant that they want them removed anyhow. Commented Sep 5, 2018 at 7:22

1 Answer 1

1

I used nulldb gem to replace the database adapter.

Sign up to request clarification or add additional context in comments.

1 Comment

did you manage to retrieve the object when testing the controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.