Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec-模型唯一性测试问题_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails RSpec-模型唯一性测试问题

Ruby on rails RSpec-模型唯一性测试问题,ruby-on-rails,rspec,Ruby On Rails,Rspec,我试图做一些简单的模型测试: /app/models/album.rb: class Album < ActiveRecord::Base has_many :slides, dependent: :restrict_with_exception validates :name, presence: true end 我原以为它会先失败(因为我在name字段上没有validates:university和索引),但它通过了。所以我改变了: 它{应该是无效的} 到 it{应

我试图做一些简单的模型测试:

/app/models/album.rb:

class Album < ActiveRecord::Base
    has_many :slides, dependent: :restrict_with_exception
    validates :name, presence: true
end
我原以为它会先失败(因为我在name字段上没有
validates:university
和索引),但它通过了。所以我改变了:

它{应该是无效的}
it{应该是有效的}

看看发生了什么,这就是我得到的:

 1) Album when album name is already taken should be valid
     Failure/Error: it { should be_valid }
       expected #<Album id: nil, name: nil, created_at: nil, updated_at: nil> to be valid, but got errors: Name can't be blank
     # ./spec/models/album_spec.rb:14:in `block (3 levels) in <top (required)>'
1)已使用唱片集名称的唱片集应有效
失败/错误:它{应该是有效的}
应为有效,但出现错误:名称不能为空
#./spec/models/album_spec.rb:14:in'block(3层)in'
我想问你我做错了什么


还有一件事是我是否可以/应该在这里使用
expect
而不是
should
语法?我在某个地方读到,
should
有点不受欢迎,不推荐使用
expect
,但我不知道如何将其用于模型测试(我的控制器/视图测试中有
expect(page)
expect(当前路径)
。模型可以使用什么参数?

在示例之前设置一个
明确主题

subject {@album}
it { should_not be_valid }

当前,根据失败错误
#
创建了
相册
的隐式空白实例,因为在示例之前未找到
显式主题

在示例之前设置一个
显式主题

subject {@album}
it { should_not be_valid }

当前,根据失败错误
#
创建了
相册
的隐式空白实例,因为在示例之前未找到
显式主题

在示例之前设置一个
显式主题

subject {@album}
it { should_not be_valid }

当前,根据失败错误
#
创建了
相册
的隐式空白实例,因为在示例之前未找到
显式主题

在示例之前设置一个
显式主题

subject {@album}
it { should_not be_valid }

目前,根据失败错误
#
创建了
相册
的隐式空白实例,因为在示例之前未找到任何
显式主题

我从未见过您使用的
it
语法。首先,我要检查此处提供的快速入门文档:然后确保您也熟悉这组文档:

从github上的示例:

require "spec_helper"

describe User do
  it "orders by last name" do
    lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
    chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")

    expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
  end
end
您可能希望将第二个
descripe
更改为
it
,然后使用一个或多个
expect
来确定测试是否通过。
it
接受测试输出中出现的字符串。因此,通常您希望使其具有表达性。此外,这里不需要使用before块。您可以这样做
it
块中的所有内容:

require 'spec_helper'

describe Album do
  it "fails validation when album name is already taken" do
    album = Album.new(name: 'Example Album')
    another_album = album.dup
    expect {another_album.save!}.to  raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active.')
  end
end

我从未见过您使用的
it
语法。首先,我要查看此处提供的快速入门文档:然后确保您也熟悉这组文档:

从github上的示例:

require "spec_helper"

describe User do
  it "orders by last name" do
    lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
    chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")

    expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
  end
end
您可能希望将第二个
descripe
更改为
it
,然后使用一个或多个
expect
来确定测试是否通过。
it
接受测试输出中出现的字符串。因此,通常您希望使其具有表达性。此外,这里不需要使用before块。您可以这样做
it
块中的所有内容:

require 'spec_helper'

describe Album do
  it "fails validation when album name is already taken" do
    album = Album.new(name: 'Example Album')
    another_album = album.dup
    expect {another_album.save!}.to  raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active.')
  end
end

我从未见过您使用的
it
语法。首先,我要查看此处提供的快速入门文档:然后确保您也熟悉这组文档:

从github上的示例:

require "spec_helper"

describe User do
  it "orders by last name" do
    lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
    chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")

    expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
  end
end
您可能希望将第二个
descripe
更改为
it
,然后使用一个或多个
expect
来确定测试是否通过。
it
接受测试输出中出现的字符串。因此,通常您希望使其具有表达性。此外,这里不需要使用before块。您可以这样做
it
块中的所有内容:

require 'spec_helper'

describe Album do
  it "fails validation when album name is already taken" do
    album = Album.new(name: 'Example Album')
    another_album = album.dup
    expect {another_album.save!}.to  raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active.')
  end
end

我从未见过您使用的
it
语法。首先,我要查看此处提供的快速入门文档:然后确保您也熟悉这组文档:

从github上的示例:

require "spec_helper"

describe User do
  it "orders by last name" do
    lindeman = User.create!(first_name: "Andy", last_name: "Lindeman")
    chelimsky = User.create!(first_name: "David", last_name: "Chelimsky")

    expect(User.ordered_by_last_name).to eq([chelimsky, lindeman])
  end
end
您可能希望将第二个
descripe
更改为
it
,然后使用一个或多个
expect
来确定测试是否通过。
it
接受测试输出中出现的字符串。因此,通常您希望使其具有表达性。此外,这里不需要使用before块。您可以这样做
it
块中的所有内容:

require 'spec_helper'

describe Album do
  it "fails validation when album name is already taken" do
    album = Album.new(name: 'Example Album')
    another_album = album.dup
    expect {another_album.save!}.to  raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active.')
  end
end

我需要添加验证和索引来确认这一点,但我支持
主题{@other_album}
应该是正确的,对吗?根据您的问题,您希望检查验证的唯一性(稍后)所以,主题应该是
@album
,因为数据库中已经保存了另一个相册
。首先
语句之前,我保存了
@album
,然后我试图将它复制为
@anbum
,并保存它(我想我应该使用
@
,这在我最初的问题中缺失).我仍然感到困惑。我将进行迁移并检查..给我一分钟你说的都是对的。我向模型添加了索引和验证,现在它工作正常。我仍然不明白为什么它不能与另一个相册一起工作,但我希望我能找到上面显示的阅读文档。我需要添加验证和索引来确认这一点,但我提供了e
subject{@other_album}
应该是正确的,对吗?根据您的问题,您希望检查验证是否具有
唯一性
(稍后)所以,主题应该是
@album
,因为数据库中已经保存了另一个相册
。首先,在
语句之前,我保存了
@album
,然后我尝试将它复制为
@another\u album
,并保存它(我没有