我有一个完整的工作项目,但我想为主要活动编写一个espresso测试,主要测试计数recyclelerview项目,代码消耗JSON值并在回收器视图的帮助下将列表传递给适配器以显示。我写了一个测试,但我继续将null对象作为响应,下面的代码失败是代码
MainActivity.class
describe('my sweet test', () => {
it('clicks it', () => {
const app = shallow(<App />)
const instance = app.instance()
const spy = jest.spyOn(instance, 'myClickFunc')
instance.forceUpdate();
const p = app.find('.App-intro')
p.simulate('click')
expect(spy).toHaveBeenCalled()
})
})
RecipeAdapter.class
public class MainActivity extends AppCompatActivity implements RecipeTestDownloader.DelayerCallback {
private RecyclerView recyclerView;
ProgressDialog pd;
List<Recipe> recipes;
private static Context mContext;
@Nullable
private SimpleIdlingResource mIdlingResource;
@VisibleForTesting
@NonNull
public IdlingResource getIdlingResource() {
if (mIdlingResource == null) {
mIdlingResource = new SimpleIdlingResource();
}
return mIdlingResource;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_list);
initViews();
mContext = this;
RecipeSyncUtils.initialize(this);
getIdlingResource();
}
public Activity getActivity(){
Context context = this;
while (context instanceof ContextWrapper){
if (context instanceof Activity){
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
return null;
}
private void initViews(){
boolean isPhone = getResources().getBoolean(R.bool.is_phone);
if (isPhone) {
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
} else {
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
}
}
RecipeSyncUtils.initialize(this);
loadJSON();
}
private void loadJSON(){
Service serviceAPI = Client.getClient();
Call<JsonArray> loadRecipeCall = serviceAPI.readRecipeArray();
loadRecipeCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
try {
String recipeString = response.body().toString();
Type listType = new TypeToken<List<Recipe>>() {}.getType();
recipes = JsonUtils.getRecipeListFromJson(recipeString, listType);
recyclerView.setAdapter(new RecipeAdapter(getApplicationContext(), recipes));
recyclerView.smoothScrollToPosition(0);
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
@Override
protected void onStart() {
super.onStart();
RecipeTestDownloader.downloadRecipe(this, MainActivity.this, mIdlingResource);
}
@Override
public void onDone(ArrayList<Recipe> recipes) {
}
}
我需要有关如何在此MainActivty上编写espresso测试的帮助,即getItemCount测试,文本匹配器或任何测试实现