2017-04-25 60 views
1

我是新來的Android儀器測試。我想檢查我的註冊活動是否與Espresso正常工作。咖啡測試失敗:AssertionFailedWithCause:希望匹配1個意圖。實際上匹配0意圖

我試圖運行它與打算和意圖了。似乎沒有工作。

以下是我的代碼:

package com.varun.project; 

import android.content.Context; 
import android.support.test.InstrumentationRegistry; 
import android.support.test.espresso.Espresso; 
import android.support.test.espresso.intent.rule.IntentsTestRule; 
import android.support.test.runner.AndroidJUnit4; 

import org.junit.Rule; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; 
import static android.support.test.espresso.action.ViewActions.typeText; 
import static android.support.test.espresso.intent.Intents.intended; 
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 
import static org.junit.Assert.assertEquals; 

/** 
* Instrumentation test, which will execute on an Android device. 
* 
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> 
*/ 
@RunWith(AndroidJUnit4.class) 
public class ExampleInstrumentedTest { 

    @Rule 
    public IntentsTestRule<Register> ResgisterTestRule = 
      new IntentsTestRule<>(Register.class, true, true); 

    @Test 
    public void useAppContext() throws Exception { 
     // Context of the app under test. 
     Context appContext = InstrumentationRegistry.getTargetContext(); 

     assertEquals("com.varun.project", appContext.getPackageName()); 
    } 

    @Test 
    public void RegisterTest(){ 
     Espresso.onView(withId(R.id.RegEmail)).perform(typeText("[email protected]"),closeSoftKeyboard()); 
     Espresso.onView(withId(R.id.RegPassword)).perform(typeText("abc123"),closeSoftKeyboard()); 

     Espresso.onView(withId(R.id.ConfirmPassword)).perform(typeText("abc123"),closeSoftKeyboard()); 

     Espresso.onView(withId(R.id.register)).perform(click()); 

     intended(hasComponent(MainActivity.class.getName())); 
    } 
} 

下面是我得到的錯誤:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents. 

IntentMatcher: has component: has component with: class name: is "com.varun.project.MainActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String 

Matched intents:[] 

Recorded intents:[] 
at dalvik.system.VMStack.getThreadStackTrace(Native Method) 
at java.lang.Thread.getStackTrace(Thread.java:580) 
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92) 
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56) 
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184) 
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158) 
at android.support.test.espresso.intent.Intents.intended(Intents.java:187) 
at android.support.test.espresso.intent.Intents.intended(Intents.java:169) 
at com.varun.project.ExampleInstrumentedTest.RegisterTest(ExampleInstrumentedTest.java:57) 
at java.lang.reflect.Method.invoke(Native Method) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) 
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879) 
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents. 

IntentMatcher: has component: has component with: class name: is "com.varun.project.MainActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String 

Matched intents:[] 

Recorded intents:[] 
at junit.framework.Assert.fail(Assert.java:50) 
at android.support.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:87) 
at android.support.test.espresso.intent.Intents.internalIntended(Intents.java:282) 
at android.support.test.espresso.intent.Intents$2.check(Intents.java:190) 
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Tests ran to completion. 

回答

0

按照您記錄您的源代碼不點火任何意向請調試是否源代碼是否觸發了這個意圖。

然後你就可以輕鬆地斷言使用

intended(hasComponent(MainActivity.class.getName())); 

或者,也許嘗試使用