我正在使用angular 2常见的http来返回一个Observable,但是当我使用嵌套的Observable调用时,我遇到的问题是我的代码喜欢网格:
this.serviceA.get().subscribe((res1: any) => {
this.serviceB.get(res1).subscribe((res2: any) => {
this.serviceC.get(res2).subscribe((res3: any) => {
})
})
})
Run Code Online (Sandbox Code Playgroud)
现在我想使用async/await来避免这种情况,但async/await只适用于Promise.我知道Observable可以转换为Promise,但据我所知,这不是一个好习惯.那我该怎么办?
顺便说一句,如果有人能给我一个示例代码来解决这个问题,那将是很好的async/await:D
当我在 android 上运行时发生了这个错误。
我正在使用 @ionic-native/push 从我的 nodejs 服务器接收推送通知。这是我的配置:Component.ts:
initPushNotification() {
const options: PushOptions = {
android: {},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.subscribe('topic').then(() => {
console.log('subscribe success to topic')
}).catch((e) => {
console.log(e)
})
pushObject.on('registration').subscribe((data: any) => {
console.log('device token -> ' + data.registrationId);
//TODO - send device token to server
});
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification)
});
pushObject.on('error').subscribe(error => …Run Code Online (Sandbox Code Playgroud) 我有一个简单的spring boot控制器,我想为其编写单元测试,但是有错误。我已经搜索了几个小时,但仍然找不到解决方案。这是代码:
HelloController.java
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String sayHello(){
return helloService.sayHello();
}
}
Run Code Online (Sandbox Code Playgroud)
HelloService.java:
@Service
public class HelloService {
public String sayHello(){
return "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
和单元测试文件:HelloControllerTest.java:
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
private HelloService helloService;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void sayHello() throws Exception {
when(helloService.sayHello()).thenReturn("thach");
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("thach"));
}
}
Run Code Online (Sandbox Code Playgroud)
但是有一个错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
at …Run Code Online (Sandbox Code Playgroud) angular ×2
typescript ×2
ionic2 ×1
ionic3 ×1
javascript ×1
observable ×1
promise ×1
spring-boot ×1
unit-testing ×1