微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 首次RSPEC测试,确保值为1或-1

新程序员在这里.我是一名正在研究Reddit克隆项目的学生.目前我已被介绍给RSPEC.我必须开始编写自己的模型测试以用于进一步的练习.有问题的模型没有创建,它将在下一个任务中.有人可以检查我是否正确完成了这项工作?

In the next checkpoint,we’ll add a Vote model. This model will
feature an inclusion validation. Inclusion validation ensures that a
Vote’s value attribute is either 1 or -1. If a Vote is initialized
with any other value,it will not save.

  1. Create VoteSpec:

规格/型号/ Vote_spec.rb

describe Vote do
  describe "validations" do
    describe "value validation" do
      it "only allows -1 or 1 as values" do
        # your expectations here
      end
    end
  end
end

Write a spec that asserts the validations work as expected.

Use RSpec’s expect().to eq() Syntax. As you may recall from the specs in
the Ruby exercises,you can assert that something should equal false
or true.

You won’t be able to run the tests because we haven’t
generated the model we’re testing.

以下是我的实施:

describe Vote do
  describe "validations" do

    before do     
      2.times { @Vote.create(value: 1) }
      3.times { @Vote.create(value: -1) }
      2.times { @Vote.create(value: 3) }
    end

    describe "value validation" do 
      it "only allows -1 or 1 as values" do
        expect ( @Vote.value ).to eq(-1)
      end

      it "only allows -1 or 1 as values" do
        expect ( @Vote.value ).to eq(1)
      end
    end
  end
end

最好的祝福.

编辑:这是一个修订版:

describe Vote do
  describe "validations" do

    before do     
      2.times { Vote.create(value: 1) }
      3.times { Vote.create(value: 0) }
      2.times { Vote.create(value: 3) }
    end

    describe "value validation" do 
      it "only allows -1 as value" do
        expect ( @Vote.value ).to eq(-1)
      end
      it "only allows 1 as value" do
        expect ( @Vote.value ).to eq(1)
      end

      it "it prohibits other values" do
        expect( @Vote.value ).to_not be_valid
      end
    end
  end
end

我也试过这个代码,它起初工作但现在在下一个任务中失败了:

require 'rails_helper'

  describe Vote do
   describe "value validation" do

      it "allows -1" do
        value = Vote.create(value: -1)
        expect(value).to be_valid
      end

      it "allows +1" do
        value = Vote.create(value: +1)
        expect(value).to be_valid
      end

      it "prohibits other values" do
        value = Vote.create(value: 0)
        expect(value).to_not be_valid
      end

    end
end

▶ rspec spec
...FFF

Failures:

  1) Vote value validation allows -1
     Failure/Error: value = Vote.create(value: -1)
     NoMethodError:
       undefined method `update_rank' for nil:NilClass
     # ./app/models/Vote.rb:12:in `update_post'
     # ./spec/models/Vote_spec.rb:7:in `block (3 levels) in <top (required)>'

  2) Vote value validation allows +1
     Failure/Error: value = Vote.create(value: +1)
     NoMethodError:
       undefined method `update_rank' for nil:NilClass
     # ./app/models/Vote.rb:12:in `update_post'
     # ./spec/models/Vote_spec.rb:12:in `block (3 levels) in <top (required)>'

  3) Vote value validation prohibits other values
     Failure/Error: expect(value).to eq(false)

       expected: false
            got: #<Vote id: nil,value: 0,user_id: nil,post_id: nil,created_at: nil,updated_at: nil>

       (compared using ==)
     # ./spec/models/Vote_spec.rb:18:in `block (3 levels) in <top (required)>'

Finished in 0.30485 seconds (files took 3.28 seconds to load)
6 examples,3 failures

Failed examples:

rspec ./spec/models/Vote_spec.rb:6 # Vote value validation allows -1
rspec ./spec/models/Vote_spec.rb:11 # Vote value validation allows +1
rspec ./spec/models/Vote_spec.rb:16 # Vote value validation prohibits other values

解决方法

在这个稍微特殊的情况下,您可以使用绝对值.
it "only allows -1 or 1 as values" do
    expect ( @Vote.value.abs ).to eq(1)
 end

原文地址:https://www.jb51.cc/ruby/265133.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐