使用Jasmine

时间:2015-10-02 11:52:58

标签: javascript angularjs unit-testing jasmine angularjs-ng-resource

在测试我的REST服务时,我收到以下错误消息:

  

错误:[$ resource:badcfg]查询   http://errors.angularjs.org/1.4.6/ $资源/ badcfg P0 =阵列&安培; P1 =对象&安培; P2 = GET&安培; P3 = HTTP%3A%2F%2Flocalhost%3A63831%2Fapi%2Fnames   在http://localhost:60694/js/angular.js(第68行)

我不知道如何解决问题。应用程序是否可能作为WebApi位于另一个localhost端口?

以下代码显示了单元测试:

describe('Test', function () {
        var $httpBackend, $rootScope, crudService, baseUrl, respValue;

        beforeEach(function () {
            module('testApp');

            inject(function (_$httpBackend_, _$rootScope_, _CrudService_, _baseUrl_) {
                $httpBackend = _$httpBackend_;
                $rootScope = _$rootScope_;
                crudService = _CrudService_;
                baseUrl = _baseUrl_;
            });
        });

        it('should make a request to the backend', function () {
            $httpBackend.whenGET('views/home.html').respond(200);
            $httpBackend.expectGET(baseUrl + '/api/names').respond(200, [{
                Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
            }]);

            $rootScope.$digest();

            crudService.getAllNames().$promise.then(function (response) {
                respValue = response;
            });

            $httpBackend.flush();

            expect(respValue).toContain([{
                Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
            }]);
        });

编辑:

我尝试了以下答案并添加了expect() - 方法,并显示以下错误消息:

  

预期[d({Id:' 1',fname:' John',lname:' Doe',年龄:30,GroupId:   1}),$ promise:Promise({$$ state:Object({status:1,pending:   undefined,value :,processScheduled:false   })},$ resolved:true]包含[Object({Id:' 1',fname:' John',   lname:' Doe',年龄:30,GroupId:1})]。

1 个答案:

答案 0 :(得分:2)

很可能这一行是问题

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-5dp"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout


    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

        <ImageView

            android:id="@+id/image"
            android:src="@drawable/pic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar

            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="parallax" />

     </android.support.design.widget.CollapsingToolbarLayout>

  </android.support.design.widget.AppBarLayout>

  <android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:layout_gravity="fill_vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        app:cardElevation="6dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/bg_main"
                android:text="scroll down to save"/>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:orientation="horizontal" >

                <ImageView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:layout_marginRight="30dp"
                    android:layout_gravity="center"
                    android:src="@drawable/account" />

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <EditText android:id="@+id/pfname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="First Name"/>
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <ImageView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:layout_marginRight="30dp"
                    android:layout_gravity="center"
                    android:src="@drawable/account" />

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <EditText android:id="@+id/plname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Last Name"/>
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <ImageView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:layout_marginRight="30dp"
                    android:layout_gravity="center"
                    android:src="@drawable/phone" />

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <EditText android:id="@+id/pphonenb"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Phone"/>
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="5dp"
                android:orientation="horizontal" >

                <ImageView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:layout_marginRight="30dp"
                    android:layout_gravity="center"
                    android:src="@drawable/email" />

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <EditText android:id="@+id/pemail"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Email"/>
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/bottom_panel"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <com.rey.material.widget.Button
                    android:id="@+id/btsave"
                    style="@style/DarkRaiseColorButtonRippleStyle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Save"
                    android:textColor="@color/white"
                    android:background="@color/bg_main"
                    app:rd_backgroundColor="@color/bg_main"
                    app:rd_enable="true"
                    />
            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>


</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    app:layout_anchor="@id/app_bar_layout"
    style="@style/fab"
    app:theme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_anchorGravity="bottom|right|end"

    android:src="@drawable/ic_photo_camera_white_24dp"
    />

</android.support.design.widget.CoordinatorLayout>

响应应该是一个数组,但它是一个对象。你能把这一行改成

吗?
$httpBackend.expectGET(baseUrl + '/api/names').respond(200, {
                Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
            });

然后试试。