json[:errors] = ["Username can't be blank", "Email can't be blank"]
en.yml中的错误本身提供为:
username: "can't be blank",
email: "can't be blank"
和测试:
expect(json[:errors]).to include t('activerecord.errors.messages.email')
失败,因为它正在查看字符串“电子邮件不能为空”,而“不能为空”与之不匹配。
我的问题是最好的测试方法(我指的是最佳实践) 子字符串包含在数组json [:errors]
中包含的字符串中答案 0 :(得分:2)
RSpec提供了一系列匹配器。在这种情况下,您需要使用<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="viewModel" type="com.example.testmediastore.MyViewModel"/>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SongFileListActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/songfile_list"/>
</FrameLayout>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/seekbar_playback"
android:progress="@{viewModel.playerProgress}"
android:max="@{viewModel.playerProgressMax}"
android:onProgressChanged="onSeekBarChanged"
app:layout_behavior="@string/bottom_sheet_behavior"/>
<fragment
android:layout_width="wrap_content"
android:layout_height="@dimen/play_control_height"
android:name="com.example.testmediastore.PlayControlFragment"
android:id="@+id/fragment"
android:layout_gravity="bottom|center"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
匹配器(docs)来检查数组的每个元素。而且,您需要使用include
正则表达式匹配器(docs)来匹配子字符串:
match
出于可读性考虑,expect(json[:errors]).to include(match(/can't be blank/))
正则表达式匹配器的别名为match
,如下所示:
a_string_matching