1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
%w[have_rendered render_template].each do |template_expectation|
RSpec.describe template_expectation do
include RSpec::Rails::Matchers::RenderTemplate
let(:response) { ActionDispatch::TestResponse.new }
context "given a hash" do
def assert_template(*); end
it "delegates to assert_template" do
expect(self).to receive(:assert_template).with({ this: "hash" }, "this message")
expect("response").to send(template_expectation, { this: "hash" }, "this message")
end
end
context "given a string" do
def assert_template(*); end
it "delegates to assert_template" do
expect(self).to receive(:assert_template).with("this string", "this message")
expect("response").to send(template_expectation, "this string", "this message")
end
end
context "given a symbol" do
def assert_template(*); end
it "converts to_s and delegates to assert_template" do
expect(self).to receive(:assert_template).with("template_name", "this message")
expect("response").to send(template_expectation, :template_name, "this message")
end
end
context "with should" do
context "when assert_template passes" do
def assert_template(*); end
it "passes" do
expect do
expect(response).to send(template_expectation, "template_name")
end.to_not raise_exception
end
end
context "when assert_template fails" do
it "uses failure message from assert_template" do
def assert_template(*)
raise ActiveSupport::TestCase::Assertion, "this message"
end
expect do
expect(response).to send(template_expectation, "template_name")
end.to raise_error("this message")
end
end
context "when fails due to some other exception" do
it "raises that exception" do
def assert_template(*)
raise "oops"
end
expect do
expect(response).to send(template_expectation, "template_name")
end.to raise_exception("oops")
end
end
end
context "with should_not" do
context "when assert_template fails" do
it "passes" do
def assert_template(*)
raise ActiveSupport::TestCase::Assertion, "this message"
end
expect do
expect(response).to_not send(template_expectation, "template_name")
end.to_not raise_exception
end
end
context "when assert_template passes" do
it "fails with custom failure message" do
def assert_template(*); end
expect do
expect(response).to_not send(template_expectation, "template_name")
end.to raise_error(/expected not to render "template_name", but did/)
end
end
context "when fails due to some other exception" do
it "raises that exception" do
def assert_template(*); raise "oops"; end
expect do
expect(response).to_not send(template_expectation, "template_name")
end.to raise_exception("oops")
end
end
context "when fails with a redirect" do
let(:response) { ActionDispatch::TestResponse.new(303) }
def assert_template(*)
message = "expecting <'template_name'> but rendering with <[]>"
raise ActiveSupport::TestCase::Assertion, message
end
def normalize_argument_to_redirection(_response_redirect_location)
"http://test.host/widgets/1"
end
it "gives informative error message" do
response = ActionDispatch::TestResponse.new(302)
response.location = "http://test.host/widgets/1"
expect do
expect(response).to send(template_expectation, "template_name")
end.to raise_exception("expecting <'template_name'> but was a redirect to <http://test.host/widgets/1>")
end
context 'with a badly formatted error message' do
def assert_template(*)
message = 'expected [] to include "some/path"'
raise ActiveSupport::TestCase::Assertion, message
end
it 'falls back to something informative' do
expect do
expect(response).to send(template_expectation, "template_name")
end.to raise_exception('expected [] to include "some/path" but was a redirect to <http://test.host/widgets/1>')
end
end
end
end
end
end
|