简体   繁体   English

Ruby on Rails 4-Rspec:计数应已更改为1,但已更改为0

[英]Ruby on Rails 4 - Rspec: count should have been changed by 1, but was changed by 0

I'm currently working on a Ruby on Rails Application and I'm new to testing. 我目前正在研究Ruby on Rails应用程序,并且是测试的新手。 A few day ago I started to write tests using the gems rspec-rails , factory_girl_rails and faker . 几天前,我开始使用gems rspec-railsfactory_girl_railsfaker编写测试。

Writing tests for model validations was quite easy, but now I'm stuck at writing controller tests. 编写用于模型验证的测试非常容易,但是现在我只能编写控制器测试。 I have a simple class "Message", which I created by using the scaffold command. 我有一个简单的类“ Message”,该类是通过使用scaffold命令创建的。

SCHEMA: 架构:

create_table "messages", force: true do |t|
  t.string   "name"
  t.string   "email"
  t.string   "text"
  t.datetime "created_at"
  t.datetime "updated_at"   end

When I try to run the following test.. 当我尝试运行以下测试时..

spec/controllers/messages_controller_spec.rb: 规格/控制器/ messages_controller_spec.rb:

  describe "POST create" do
    describe "with valid params" do
      it "creates a new Message" do
        expect {
          post :create, FactoryGirl.create(:message)
        }.to change(Message, :count).by(1)
      end
    end
  end

..I get this error message.. ..我收到此错误消息。

MessagesController POST create with valid params creates a new Message (FAILED - 1) 使用有效参数创建的MessagesController POST创建新消息(FAILED-1)

Failures: 失败:

1) MessagesController POST create with valid params creates a new Message Failure/Error: expect { count should have been changed by 1, but was changed by 0 1)使用有效参数创建的MessagesController POST将创建新的消息失败/错误:期望{计数应该已被更改为1,但被更改为0

..by using this factories: ..通过使用此工厂:

spec/factories/message.rb: 规格/工厂/ message.rb:

require "faker"

FactoryGirl.define do
  factory :message do |m|
    m.name { Faker::Name.name }
    m.email { Faker::Internet.email }
    m.text { Faker::Lorem.sentences }
  end
end

I've been searching HOURS for an answer - but so far I haven't found a solution that works for me. 我一直在寻找HOURS的答案-但到目前为止,我还没有找到适合我的解决方案。 I hope anyone out there can help me! 我希望那里的任何人都能帮助我!

Thanks! 谢谢!

This is my Model: (But even if I remove all validations I get the same error message) 这是我的模型:(但即使删除所有验证,我也会收到相同的错误消息)

class Message < ActiveRecord::Base
  validates :name, :email, :text,
    presence: { 
      value:  :true,
      message: "ist ein Pflichtfeld"
    }

    validates :name, length: { maximum: 50,
        too_long: "ist zu lang" }

    validates :email, length: { maximum: 50,
        too_long: "ist zu lang" }

    validates :text, length: { maximum: 10000,
        too_long: "ist zu lang" }

end

This is my create action: 这是我的创建动作:

  def create

    @message = Message.new(message_params)
    respond_to do |format|
      if @message.save
        format.html { redirect_to action: 'new' }
        format.json { render action: 'show', status: :created, location: @message }
      else
        format.html { render action: 'new' }
        format.json { render json: @message.errors, status: :unprocessable_entity }
      end
    end
  end

Function: message_params 函数:message_params

def message_params
  params.require(:message).permit(:name, :email, :text)
end

Use attributes_for method which returns a hash of attributes that can be used to build an instance of Message. 使用attributes_for方法,该方法返回可用于构建Message实例的属性的哈希。

 describe "POST create" do
    describe "with valid params" do
      it "creates a new Message" do
        expect {
          post :create, FactoryGirl.attributes_for(:message) ## Use attributes_for
        }.to change(Message, :count).by(1)
      end
    end
  end

FactoryGirl.create(:message) would actually save a record in the messages table. FactoryGirl.create(:message)实际上会在messages表中保存一条记录。 For controller testing, you just have to pass a hash of attributes and post :create would call the MessagesController#create action which in turn would save the record in database. 对于控制器测试,您只需要传递属性的哈希值,然后post :create将调用MessagesController#create操作,该操作将把记录保存在数据库中。

EDIT 编辑

Replace m.text { Faker::Lorem.sentences } with m.text { Faker::Lorem.paragraph } . m.text { Faker::Lorem.sentences }替换为m.text { Faker::Lorem.paragraph } As sentences returns an Array of Strings whereas field text in Model message requires a String. sentences返回一个字符串数组,而“模型” message字段text需要一个字符串。 This will result in an error and fail to save the record. 这将导致错误,并且无法保存记录。

require "faker"

FactoryGirl.define do
  factory :message do |m|
    m.name { Faker::Name.name }
    m.email { Faker::Internet.email }
    m.text { Faker::Lorem.paragraph } ## Use paragraph instead of sentences
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM