小编tho*_*omi的帖子

Angular Ngrx 测试:Store.overrideSelector 不会覆盖

我确实有以下内容beforeEach

beforeEach(() => {
    fixture = TestBed.createComponent(ApplyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    store = TestBed.get<Store<fromAnnouncement.AnnouncementState>>(Store);
    store.refreshState();
    mockApplicationSelector = store.overrideSelector(selectDraftApplication, testDraftApplication); <-- Try to mock  a selector here.
  });
Run Code Online (Sandbox Code Playgroud)

选择器 selectDraftApplication 取决于 的功能选择器AnnouncementState,其中有两个实体,其中之一是我使用功能选择器进行子选择以构建我的selectDraftApplication选择器。

当在测试中使用这段代码时,我所有的测试都会崩溃,因为 NgRx 似乎仍然在寻找进一步向上的所有状态,而我希望模拟能够准确地防止这种情况发生。模拟整个 ngrx 实体存储是没有意义的,所以我只希望选择器准确地返回该对象并完成它。我用谷歌搜索了很远很远,但没有得到任何答案。有什么帮助吗?我运行 Angular 8 和 ngrx 8.5.1(该refreshState()功能也不起作用......)

谢谢!

编辑

export const getAnnouncementState = createFeatureSelector<AnnouncementState>('announcement');

export const getApplicationsState = createSelector(
  getAnnouncementState,
  (state: AnnouncementState) => state.applications 
);

export const selectDraftApplication = createSelector(
  getApplicationsState,
  (state: ApplicationState) => state.draftApplication
);
Run Code Online (Sandbox Code Playgroud)

unit-testing ngrx angular angular-test

8
推荐指数
1
解决办法
1万
查看次数

使用 #authentication 实用程序的 Spring Boot 和 Thymeleaf

使用 Spring Boot、spring security starter 和 thymeleaf,登录后我无法访问 #authentication 实用程序(更准确地说,它为空)。我没有做任何特殊的配置(假设初学者会为我做这件事)并且我没有在我的模板中包含 sec: 命名空间(同样,假设我不需要它 - 到目前为止我看到的所有例子都没有)也不需要)。我想这样称呼:{#authentication.expression('isAuthenticated()')}

作为参考,这是在身份验证后调用的控制器:

import java.security.Principal;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/site-admin")
public class VZSiteAdminController {

    @RequestMapping(method=RequestMethod.GET)
    public String mainScreen(Principal principal){

    return "site-admin";

    }
}
Run Code Online (Sandbox Code Playgroud)

java spring-security thymeleaf spring-boot

5
推荐指数
2
解决办法
1万
查看次数

在Spock规范中注入时,WebApplicationContext不会自动装配

虽然我在尝试时遵循了Spring Boot Guide:

@SpringApplicationConfiguration(classes=MainWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class)
@WebAppConfiguration
@ActiveProfiles("integration-test")

class FirstSpec extends Specification{
  @Autowired
  WebApplicationContext webApplicationContext

  @Shared
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()

  def "Root returns 200 - OK"(){

      when:
      response = mockMvc.perform(get("/"))

      then:
      response.andExpect(status().isOk())
  }
}
Run Code Online (Sandbox Code Playgroud)

我只是得到了一个消息,即没有注入WebApplicationContext.我有

    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-spring</artifactId>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-maven</artifactId>
        <version>0.7-groovy-2.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

在我的.pom中,正如指南建议的那样,但仍然没有成功.我错过了什么?我需要应用程序上下文,因此所有bean都被注入.有任何想法吗?

spring maven spock spring-boot mockmvc

5
推荐指数
1
解决办法
851
查看次数

@SpringBootTest的问题:上下文没有加载,Spock无法@Autowire

我刚刚转到Spring Boot 1.4.0来使用新的测试功能.但是,我似乎无法将它们与Spock Framework结合使用.请考虑以下规范:

@SpringBootTest(classes=[MainWebApplication.class], webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
class RestTemplateExampleSpec extends Specification{

@Autowired
private TestRestTemplate restTemplate

def "Web application is Reachable and Status is 200 - OK"(){

    when: "The root address is called"
    def response = restTemplate.getForObject("/", String.class)

    then: "The status code is 200 - OK"
    response.statusCode == HttpStatus.OK

}

def "Endpoint /admin/users is available"(){

    when: "/admin/users is called"
    def response = restTemplate.getForEntity("/admin/users", String.class)

    then: "The status code is 200 - OK"
    response.statusCode == HttpStatus.OK

    }

}
Run Code Online (Sandbox Code Playgroud)

问题是注释似乎甚至无法找到MainWebApplication.class(坐在层次结构中较高的包上),因此,没有上下文,所以没有任何内容Autowire …

spring spring-test spock spring-boot

5
推荐指数
1
解决办法
3353
查看次数