无法在Jasmine测试中访问角度服务功能

时间:2016-04-22 22:12:05

标签: javascript angularjs unit-testing jasmine karma-jasmine

我是Jasmine Testing和Angular框架的新手。我目前有一种奇怪的情况,我无法从我的Jasmine测试中引用我的服务功能。

我的角色服务初始化的一部分:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-jaxrs-server</artifactId>
  <packaging>jar</packaging>
  <name>swagger-jaxrs-server</name>
  <version>1.0.0</version>
  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty-version}</version>
        <configuration>
          <webApp>
            <contextPath>/</contextPath>
          </webApp>
          <webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
          <stopPort>8079</stopPort>
          <stopKey>stopit</stopKey>
          <httpConnector>
            <port>8080</port>
            <idleTimeout>60000</idleTimeout>
          </httpConnector>
        </configuration>
        <executions>
          <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
            <configuration>
              <scanIntervalSeconds>0</scanIntervalSeconds>
              <daemon>true</daemon>
            </configuration>
          </execution>
          <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.9.1</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/gen/java</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>com.thetransactioncompany</groupId>
        <artifactId>cors-filter</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-jersey-jaxrs</artifactId>
      <version>${swagger-core-version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j-version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-json</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-servlet</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey.contribs</groupId>
      <artifactId>jersey-multipart</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-server</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servlet-api-version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>${jersey-version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8.8</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <artifactId>junit</artifactId>
          <groupId>junit</groupId>
        </exclusion>
        <exclusion>
          <artifactId>snakeyaml</artifactId>
          <groupId>org.yaml</groupId>
        </exclusion>
        <exclusion>
          <artifactId>bsh</artifactId>
          <groupId>org.beanshell</groupId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>sonatype-snapshots</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>
  <properties>
    <swagger-core-version>1.5.7</swagger-core-version>
    <jetty-version>9.2.9.v20150224</jetty-version>
    <jersey-version>1.18.1</jersey-version>
    <slf4j-version>1.6.3</slf4j-version>
    <junit-version>4.8.1</junit-version>
    <servlet-api-version>2.5</servlet-api-version>
  </properties>
</project>

现在进入我的业力测试:

var service = {
checkIfCurrentIsObject: checkIfCurrentIsObject,
removeFromCurrentObject: removeFromCurrentObject;
}

function checkIfCurrentIsObject() {
          return getCurrent().isObject();
       }

function removeFromCurrentIsObject() {
         checkIfCurrentIsObject();
          return specialFunction();
       }

现在我遇到的问题是,我的spyOn没有接受我的checkIfCurrentIsObject函数,除非我在describe('when current object is property', function() { it('verify the requote function is called', function() { spyOn(Service, 'checkIfCurrentIsObject').and.returnValue(true); spyOn(Service, 'removeFromCurrentIsObject').and.callThrough(); spyOn(Service, 'specialFunction'); expect(Service, 'specialFunction').toHaveBeenCalled(); }); 函数中将其指定为this.checkIfCurrentIsObject。这有什么特别的原因吗?

1 个答案:

答案 0 :(得分:0)

是的,它应该是

 function removeFromCurrentIsObject() {
     this.checkIfCurrentIsObject();
     return this.specialFunction();
 }

监视他们。

正在监视Service.checkIfCurrentIsObject方法。在JavaScript中,无法跟踪checkIfCurrentIsObject()本地函数调用的位置。

相关问题