1

In Rails app, I am Writing Rspec Test cases.

I have a controller file, which has a method called 'index' and this returns some values to the views file which is used as a header value to some table in that particular views file. The code example is as follows.

def index @some_variable = [I18n.t('controllerFileName.index.someTitle1'), I18n.t('controllerFileName.index.someTitle2')] end

The sometitle1 and sometitle2 is going as an header to a view file.

I wanted to write a test case in controller to test whether the controller returns the correct expected value to the views file.(i.e. Sometitle1 and sometitle2 ) using Rspec.

I know that we can check the headers presence in views file using Rspec Test case. Just curious to know whether we can check the return values in controller file using Rspec.

I am new to Rspec. If it is not possible or there's a way to do this, please let me know.

1 Answer 1

2

You can test it using assigns:

get :index
expect(assigns(:some_variable)).to eq(['Title 1', 'Title 2'])

ref: https://relishapp.com/rspec/rspec-rails/docs/controller-specs

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

2 Comments

Hi, I tried this method. But I am getting the following error, Failure/Error: get :index ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"some_files"}
the get :index is just an example. The 2nd line is how you check the values returned by 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.