失败/错误:使用Rspec发布#create testing

时间:2017-12-20 20:59:38

标签: ruby-on-rails rspec

我是Rspec的新手并且测试一般我在stores_controller_spec.rb文件中有这个代码

require 'rails_helper'

RSpec.describe StoresController, type: :controller do
    let(:vendor) { FactoryBot.create(:vendor) }
    let(:auth_headers) { vendor.create_new_auth_token }
    let(:city) { FactoryBot.create(:city) }
    let(:store) { FactoryBot.create(:store, vendor_id: vendor.id, city_id: city.id) }

    before(:example) do
        request.headers.merge!(auth_headers)
    end

    describe "Post#create" do
        it 'creates a new store' do
            post :create, params: { store: attributes_for(:store) }
            expect(Store.count).to eq(1)
        end
    end
end

factories/store.rb文件:

FactoryBot.define do
    factory :store do
        long Faker::Number.positive
        lat Faker::Number.positive
        store_name Faker::Name.name  
        telephone_number Faker::Number.number(10)
    end
end

factories/city.rb文件:

FactoryBot.define do
    factory :city do
        city_name Faker::Name.name
    end
end

运行测试后,我得到了这个:

Failures:
    1) StoresController Post#create creates a new store    
    Failure/Error: expect(Store.count).to eq(1)

   expected: 1
        got: 0

StoresContoller.rb

class StoresController < ApplicationController
    before_action :authenticate_vendor!

    def create
      @store = Store.new(store_params)
      if @store.save
        render json: @store, status: :created, location: @store
      else
        render json: @store.errors, status: :unprocessable_entity
      end
    end

    private
      def store_params
        params.require(:store).permit(:vendor_id, :city_id, :store_name, :long, :lat, :telephone_number)
      end
end

我应该提到Store belongs_to :city和   belongs_to :vendor 当我在expect(store).to be_valid行之后在同一个块上测试post :create时,测试通过了!

你能解释一下我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

我相信您不需要在请求中的密钥NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext; NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:context]; [object setValue:@"infosys" forKey:@"cname"]; [object setValue:@"01" forKey:@"compID"]; [object setValue:@"Pune" forKey:@"locaation"]; [object setValue:@"wipro" forKey:@"cname"]; [object setValue:@"02" forKey:@"compID"]; [object setValue:@"Mumbai" forKey:@"locaation"]; NSError *error; if (![context save:&error]) { NSLog(@"Failed to save - error: %@", [error localizedDescription]); } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSArray *result = [context executeFetchRequest:fetchRequest error:&error]; if (result.count<=0) { NSLog(@"No User Found"); } else{ NSString *cname; NSString *compID; NSString *locaation; for (NSManagedObject *obj in result) { cname=[obj valueForKey:@"cname"]; compID=[obj valueForKey:@"compID"]; locaation=[obj valueForKey:@"locaation"]; } NSLog(@"Store Data = %@",[NSString stringWithFormat:@"%@ %@ %@",cname,compID,locaation]); } 下传递商店属性。更改此行,看看它是否有效:

params

此外,您的工厂post :create, { store: attributes_for(:store).merge(city_id: city.id, vendor_id: vendor.id) } 无法在不进行更改的情况下创建有效记录:

:store