Rails 5.2 mini test refactor share controller tests












0














I am new to testing and am using mini test.
As an example I have a controller action which will be authorised depending on wether the user has permission to access the action.



So I am running the same assertions on the same action for 4 different type of users.
I am sure this can be refactored but not sure how
Similar assertions will be also made across controllers



require 'test_helper'
class ResourcesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

setup do
@recording = recordings(:first_event_rec_1)
@user= users(:non_admin)
end

test "request index not signed in" do
#@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_redirected_to new_user_session_url(format: 'js')

end

test "request index by non admin" do
sign_in @user
@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

################################################################################################################################
################################################################################################################################

test "request index by full welsh admin but signed into german admin centre" do
@user= users(:welsh_admin)
@user.centre= centres(:germany)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

test "request index by full welsh admin" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :success
end

test "request index by full welsh admin without public role" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
@user.assignments.where(centre_id:2, role_id: 3).destroy_all
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
#######################################################################################################################
#######################################################################################################################

test "request index by full german admin but signed into welsh admin centre " do
@user= users(:german_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
end









share|improve this question




















  • 1




    Please reformat your code.
    – shizhen
    Nov 13 '18 at 3:02
















0














I am new to testing and am using mini test.
As an example I have a controller action which will be authorised depending on wether the user has permission to access the action.



So I am running the same assertions on the same action for 4 different type of users.
I am sure this can be refactored but not sure how
Similar assertions will be also made across controllers



require 'test_helper'
class ResourcesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

setup do
@recording = recordings(:first_event_rec_1)
@user= users(:non_admin)
end

test "request index not signed in" do
#@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_redirected_to new_user_session_url(format: 'js')

end

test "request index by non admin" do
sign_in @user
@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

################################################################################################################################
################################################################################################################################

test "request index by full welsh admin but signed into german admin centre" do
@user= users(:welsh_admin)
@user.centre= centres(:germany)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

test "request index by full welsh admin" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :success
end

test "request index by full welsh admin without public role" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
@user.assignments.where(centre_id:2, role_id: 3).destroy_all
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
#######################################################################################################################
#######################################################################################################################

test "request index by full german admin but signed into welsh admin centre " do
@user= users(:german_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
end









share|improve this question




















  • 1




    Please reformat your code.
    – shizhen
    Nov 13 '18 at 3:02














0












0








0







I am new to testing and am using mini test.
As an example I have a controller action which will be authorised depending on wether the user has permission to access the action.



So I am running the same assertions on the same action for 4 different type of users.
I am sure this can be refactored but not sure how
Similar assertions will be also made across controllers



require 'test_helper'
class ResourcesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

setup do
@recording = recordings(:first_event_rec_1)
@user= users(:non_admin)
end

test "request index not signed in" do
#@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_redirected_to new_user_session_url(format: 'js')

end

test "request index by non admin" do
sign_in @user
@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

################################################################################################################################
################################################################################################################################

test "request index by full welsh admin but signed into german admin centre" do
@user= users(:welsh_admin)
@user.centre= centres(:germany)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

test "request index by full welsh admin" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :success
end

test "request index by full welsh admin without public role" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
@user.assignments.where(centre_id:2, role_id: 3).destroy_all
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
#######################################################################################################################
#######################################################################################################################

test "request index by full german admin but signed into welsh admin centre " do
@user= users(:german_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
end









share|improve this question















I am new to testing and am using mini test.
As an example I have a controller action which will be authorised depending on wether the user has permission to access the action.



So I am running the same assertions on the same action for 4 different type of users.
I am sure this can be refactored but not sure how
Similar assertions will be also made across controllers



require 'test_helper'
class ResourcesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

setup do
@recording = recordings(:first_event_rec_1)
@user= users(:non_admin)
end

test "request index not signed in" do
#@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_redirected_to new_user_session_url(format: 'js')

end

test "request index by non admin" do
sign_in @user
@user.centre= centres(:wales)
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

################################################################################################################################
################################################################################################################################

test "request index by full welsh admin but signed into german admin centre" do
@user= users(:welsh_admin)
@user.centre= centres(:germany)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end

test "request index by full welsh admin" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :success
end

test "request index by full welsh admin without public role" do
@user= users(:welsh_admin)
@user.centre= centres(:wales)
sign_in @user
@user.assignments.where(centre_id:2, role_id: 3).destroy_all
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
#######################################################################################################################
#######################################################################################################################

test "request index by full german admin but signed into welsh admin centre " do
@user= users(:german_admin)
@user.centre= centres(:wales)
sign_in @user
get recording_resources_url(recording_id: @recording.id), xhr: true
assert_response :forbidden
assert_equal "app cont You don't have access to this resource.<BR> Are you logged into your admin sections?", flash[:error]
end
end






ruby-on-rails minitest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 6:07









lagom

3,46181937




3,46181937










asked Nov 13 '18 at 1:06









russell

61




61








  • 1




    Please reformat your code.
    – shizhen
    Nov 13 '18 at 3:02














  • 1




    Please reformat your code.
    – shizhen
    Nov 13 '18 at 3:02








1




1




Please reformat your code.
– shizhen
Nov 13 '18 at 3:02




Please reformat your code.
– shizhen
Nov 13 '18 at 3:02












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272309%2frails-5-2-mini-test-refactor-share-controller-tests%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272309%2frails-5-2-mini-test-refactor-share-controller-tests%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine