0

按照此处的答案,我尝试检查是否选择了某个微调器文本。微调器出现在对话框中,所以我尝试了:

onView(withId(R.id.package_spinner)).inRoot(isDialog()).check(matches(withSpinnerText(containsString("sachet"))));

但是,这不起作用,我收到以下错误消息:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: a string contains "sachet"' 与所选视图不匹配。预期:带有文本:包含“sachet”的字符串得到:“AppCompatSpinner{id=2131624039, res-name=package_spinner, visibility=VISIBLE, width=620, height=75, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is- layout-requested=false, has-input-connection=false, x=0.0, y=75.0, child-count=1}”

“is-selected=false”是否意味着它找到的微调器未被选中?这是在具有 API 18 的真实设备(不是模拟器)上运行的。在 Espresso 运行期间以及手动测试时,微调器被正确设置为“sachet”。为什么 Espresso 有问题?

不确定这是否相关,但微调器适用于以下类型的对象:

public class PackageType {
    private int id;
    private String name;

    private final Context ctx;

    public PackageType(Context context) { this.ctx=context; }
    public PackageType(String name, Context context) {
        super();
        this.ctx = context;
        setName(name);
    }

    // setters
    public void setId(int i) { this.id = i; }
    public void setName(String u) {
        this.name = u;
    }


    // getters
    public int getId() { return id; }
    public String getName() { return name; }
}

微调器适配器看起来像:

class SpinnerPackageTypeAdapter extends ArrayAdapter<PackageType> {
    private final List<PackageType> packageTypes;
    private final Context mContext;

    public SpinnerPackageTypeAdapter(Context context, int resource, List<PackageType> packageTypes) {
        super(context, resource, packageTypes);
        this.mContext = context;
        this.packageTypes = packageTypes;
    }

    public PackageType getItem(int position) { return packageTypes.get(position); }

    public long getItemId(int position) { return position; }

    // this is for the passive state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // use dynamically created TextView, but could reference custom layout
        TextView label = new TextView(mContext);
        label.setTextColor(Color.BLACK);
        label.setTextSize(mContext.getResources().getDimension(R.dimen.list_row_font_size));
        label.setGravity(Gravity.CENTER);
        label.setText(getItem(position).getName());

        return label;
    }

    // this is for the chooser dropped down spinner
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView label = (TextView) View.inflate(mContext,R.layout.row_spinner,null);
        label.setText(getItem(position).getName());
        return label;
    }

}
4

1 回答 1

0

我发现了问题。在类 PackageType 中,我必须重写 toString() 方法(当然,只要“名称”在那里,其他版本也是可能的):

@Override
    public String toString() {
        return "PackageType [id=" + id + ", name=" + name + "]";
    }
于 2015-12-08T12:04:10.060 回答