Jeffrey M Castro
Jeffrey M Castro

Reputation: 377

Am I targeting the correct specs for a controller Request spec?

I have a sample controller with the following action, and am writing a Request spec for it:

def subscribe_user
  concept = UsersConcept::Subscribe.new(user_params)
  concept.run

  respond_to do |format|
    format.html { redirect_to users_path, notice: concept.notice_msg }
  end
end

private

def user_params
  params.require(:user).permit(:id, :channel_id)
end

Am I correct to write tests for the following:

  1. UsersConcept::Subscribe calls new
  2. the result from #1 above calls run

Or should I just focus on writing test for verifying redirects, error messages, etc. Thanks in advance.

Upvotes: 2

Views: 22

Answers (1)

Simon Franzen
Simon Franzen

Reputation: 2727

Stop writing controller tests, make Request specs

Rails and RSpec teams suggest replacing or removing your app’s controller tests (also known as the functional test layer), in favor of directly testing models (units), or with higher-level integration tests.

Here you can find the article and some example code:

https://everydayrails.com/2016/08/29/replace-rspec-controller-tests.html

For your action you could test the redirection, success or error messages, a change in die DB for your subscription table...

Upvotes: 1

Related Questions