Update should helper with more functionality

- Allow for different user levels by checking the CurrentUser variable
- Allow for other URL parameters other than the search parameters
-- This is needed on some controllers such as the comments controller
- Allow for delayed execution of lambda functions for item input
-- This is because the updated_at field isn't correct at the time
   that the input gets sent to the function
-- This prevented the right order from being tested
This commit is contained in:
BrokenEagle
2020-07-19 04:02:47 +00:00
parent fc5e003e28
commit 34ca33e22f

View File

@@ -9,28 +9,37 @@ module ControllerHelper
# setup { @touhou = create(:tag, name: "touhou") }
# should respond_to_search(name: "touhou").with { @touhou }
#
def respond_to_search(search_params)
RespondToSearchMatcher.new(search_params)
def respond_to_search(search_params, other_params: {})
RespondToSearchMatcher.new(search_params, other_params)
end
class RespondToSearchMatcher < Struct.new(:params)
class RespondToSearchMatcher < Struct.new(:params, :other_params)
def description
"should respond to a search for #{params}"
end
def matches?(subject, &block)
search_params = { search: params }
search_params = other_params.merge({ search: params })
expected_items = @test_case.instance_eval(&@expected)
@test_case.instance_eval do
# calls e.g. "wiki_pages_path" if we're in WikiPagesControllerTest.
index_url = send("#{subject.controller_path}_path")
get index_url, as: :json, params: search_params
# Allows for different authorization levels to be used, instead of just anonymous
if CurrentUser.user.present?
get_auth index_url, CurrentUser.user, as: :json, params: search_params
else
get index_url, as: :json, params: search_params
end
# Don't continue processing if there was an error
assert_response :success
# Some fields like :updated_at do not get finalized until later, so allow lambda functions
# to evaluate expressions like a sort after the network call has completed
expected_items = expected_items.call if expected_items.respond_to?(:call)
expected_ids = Array(expected_items).map(&:id)
responded_ids = response.parsed_body.map { |item| item["id"] }
assert_response :success
assert_equal(expected_ids, responded_ids)
end
end