简体   繁体   English

RSpec-模型唯一性测试问题

[英]RSpec - model uniqueness testing issue

I'm trying to make some simple model test: 我正在尝试进行一些简单的模型测试:

/app/models/album.rb: /app/models/album.rb:

class Album < ActiveRecord::Base
    has_many :slides, dependent: :restrict_with_exception
    validates :name, presence: true
end

/spec/model/album_spec.rb: /spec/model/album_spec.rb:

require 'spec_helper'

describe Album do
    before do
        @album = Album.new(name: 'Example Album')
    end

    describe "when album name is already taken" do
        before do
            another_album = @album.dup
            another_album.save
        end

        it { should_not be_valid }
    end
end

I was expecting it to fail first (as I have no validates :uniqueness and index on the name field) but it passed. 我期望它首先失败(因为我没有在名称字段上validates :uniqueness和index),但是它通过了。 So I changed: 所以我改变了:

it { should_not be_valid } to it { should be_valid } it { should_not be_valid } it { should be_valid }

To see what's going on and this is what I got: 看看发生了什么,这就是我得到的:

 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)>'

I would like to ask you what I did wrong. 我想问你我做错了什么。

One more thing is if I can/should use expect rather than should syntax here ? 还有一件事是,如果我可以/应该使用expect ,而不是should在这里语法? I read somewhere that should is a bit deprecated and not expect is recomended but I don't know how to use it for model testing (I have it on my Controller/View test in form of expect(page) or expect(current_path) . What argument can I use for model ? 我读的地方, should是有点过时,不expect被recomended,但我不知道如何使用它进行模型试验(我有它在的形式,我的控制器/视图测试expect(page)expect(current_path)我可以为模型使用什么参数?

I have never seen the it syntax that you are using. 我从未见过您使用的it语法。 First thing, I would checkout the quick start documentation available here: https://github.com/rspec/rspec-rails#model-specs and then make sure that you are familiar with this set of docs as well: http://rspec.info/ 首先,我将在此处查看快速入门文档: https : //github.com/rspec/rspec-rails#model-specs ,然后确保您也熟悉这套文档: http:// rspec.info/

From the example on github: 从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

You would want to change your second describe to an it and then use one or more expect to determine if the test passes. 您可能希望将第二个describe更改为it ,然后使用一个或多个expect来确定测试是否通过。 it takes a string that appears in the test output. it需要一个出现在测试输出中的字符串。 So generally you want to make it something expressive. 因此,通常您希望使其具有表现力。 Additionally there is no need to use before blocks here. 另外,这里不需要在块之前使用。 You can do everything in the it block: 您可以在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

Setup an explicit subject before your example: 在您的示例之前设置一个explicit subject

subject {@album}
it { should_not be_valid }

Currently, as per the failure error #<Album id: nil, name: nil, created_at: nil, updated_at: nil> an implicit blank instance of Album is created as no explicit subject is found before the example. 当前,根据失败错误#<Album id: nil, name: nil, created_at: nil, updated_at: nil> ,创建了Album的隐式空白实例,因为在该示例之前未找到explicit subject

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

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