Rails 上的 Ruby - minitest assert_select 测试通过任何已解析的 JSON,即使它应该失败
Ruby on Rails - minitest assert_select test passes with any parsed JSON, even when it should fail
我似乎无法使用虚假数据使我的测试失败,所以这告诉我我没有正确测试我的实际数据。我的视图显示已解析的 JSON 响应(我已在浏览器中验证),但我似乎无法在测试中验证它。
# Arrange
account = accounts(:good_account) # from fixture
expected_data = { "status": "SUCCESS" }.to_json
phony_data = { "bestBearType": "BLACK" }.to_json
# Act
get some_show_url(account.id)
# Assert
assert_select 'p', JSON.parse(expected_data) # passes
assert_select 'p', JSON.parse(phony_data) # passes (shouldn't)
assert_select 'p', { "bestBearType": "BLACK" } # passes (shouldn't)
assert_select 'p', expected_data # fails
我真的很想知道虚假数据是如何传递的。就好像我只有 assert_select 'p'
而没有后面的预期值一样。在此处使用已解析的 JSON 是否存在问题?
Is there an issue using parsed JSON here?
您误用了 assert_select
,这与 JSON 无关,它用于从您的服务器输出中选择 HTML 元素。我不知道你在这里的意图是什么,但你不能查询已解析(或未解析)的 JSON 到 assert_select
,或将 JSON 作为其任何参数发送。
在您的四种情况中,assert_select
的行为都符合预期:
assert_select 'p', JSON.parse(expected_data) # passes
您向它传递了一个未知选项,"status"
。这不是 assert_select
支持的选项,所以实际上你是 运行 assert_select 'p'
,它通过了,因为它找到了 <p>
.
当给定哈希作为其第二个参数时,它理解的选项是:
:text - Narrow the selection to elements that have this text value
(string or regexp).
:html - Narrow the selection to elements that have this HTML content
(string or regexp).
:count - Assertion is true if the number of selected elements is equal
to this value.
:minimum - Assertion is true if the number of selected elements is at
least this value.
:maximum - Assertion is true if the number of selected elements is at
most this value.
assert_select 'p', JSON.parse(phony_data) # passes (shouldn't)
是的,这应该通过,原因相同:您正在传递一个被忽略的随机未知选项,并且发现 <p>
匹配。
assert_select 'p', { "bestBearType": "BLACK" } # passes (shouldn't)
如上所述,是的,这应该通过,"bestBearType" 不是 assert_select
的有效选项。
assert_select 'p', expected_data # fails
这个应该失败,因为你给它的第二个参数是一个字符串,匹配如下:
Assertion is true if the text value of at least one element matches the string or regular expression.
输出中没有 <p>
标记匹配字符串 "{\"status\": \"SUCCESS\"}"
.
有关完整说明和一些有用示例,请参阅 the documentation。
我似乎无法使用虚假数据使我的测试失败,所以这告诉我我没有正确测试我的实际数据。我的视图显示已解析的 JSON 响应(我已在浏览器中验证),但我似乎无法在测试中验证它。
# Arrange
account = accounts(:good_account) # from fixture
expected_data = { "status": "SUCCESS" }.to_json
phony_data = { "bestBearType": "BLACK" }.to_json
# Act
get some_show_url(account.id)
# Assert
assert_select 'p', JSON.parse(expected_data) # passes
assert_select 'p', JSON.parse(phony_data) # passes (shouldn't)
assert_select 'p', { "bestBearType": "BLACK" } # passes (shouldn't)
assert_select 'p', expected_data # fails
我真的很想知道虚假数据是如何传递的。就好像我只有 assert_select 'p'
而没有后面的预期值一样。在此处使用已解析的 JSON 是否存在问题?
Is there an issue using parsed JSON here?
您误用了 assert_select
,这与 JSON 无关,它用于从您的服务器输出中选择 HTML 元素。我不知道你在这里的意图是什么,但你不能查询已解析(或未解析)的 JSON 到 assert_select
,或将 JSON 作为其任何参数发送。
在您的四种情况中,assert_select
的行为都符合预期:
assert_select 'p', JSON.parse(expected_data) # passes
您向它传递了一个未知选项,
"status"
。这不是assert_select
支持的选项,所以实际上你是 运行assert_select 'p'
,它通过了,因为它找到了<p>
.当给定哈希作为其第二个参数时,它理解的选项是:
:text - Narrow the selection to elements that have this text value (string or regexp).
:html - Narrow the selection to elements that have this HTML content (string or regexp).
:count - Assertion is true if the number of selected elements is equal to this value.
:minimum - Assertion is true if the number of selected elements is at least this value.
:maximum - Assertion is true if the number of selected elements is at most this value.
assert_select 'p', JSON.parse(phony_data) # passes (shouldn't)
是的,这应该通过,原因相同:您正在传递一个被忽略的随机未知选项,并且发现
<p>
匹配。assert_select 'p', { "bestBearType": "BLACK" } # passes (shouldn't)
如上所述,是的,这应该通过,"bestBearType" 不是
assert_select
的有效选项。assert_select 'p', expected_data # fails
这个应该失败,因为你给它的第二个参数是一个字符串,匹配如下:
Assertion is true if the text value of at least one element matches the string or regular expression.
输出中没有
<p>
标记匹配字符串"{\"status\": \"SUCCESS\"}"
.
有关完整说明和一些有用示例,请参阅 the documentation。