Ruby rspec投票验证错误:必须将哈希作为参数传递

Ruby rspec投票验证错误:必须将哈希作为参数传递,ruby,rspec,Ruby,Rspec,我正在尝试为投票规格模型编写规格代码。不确定到底是什么我做错了。我想它可能在before块的第一个@vote属性中 以下是验证的工作方式: Console v = Vote.new(value: 1) v.valid? #=> true v2 = Vote.new(value: -1) v2.valid? #=> true v3 = Vote.new(value: 2) v3.valid? #=> false 这就是错误: Failure/Error: @vote = V

我正在尝试为投票规格模型编写规格代码。不确定到底是什么我做错了。我想它可能在before块的第一个@vote属性中

以下是验证的工作方式:

Console
v = Vote.new(value: 1)
v.valid? #=> true

v2 = Vote.new(value: -1)
v2.valid? #=> true

v3 = Vote.new(value: 2)
v3.valid? #=> false
这就是错误:

Failure/Error: @vote = Vote.create(:post_id)
 ArgumentError:
   When assigning attributes, you must pass a hash as an argument.
这是我的投票

require 'rails_helper'

describe Vote do

describe "validations" do

    before do
        @vote = Vote.create(:post_id)
        @vote.create(value: 1)
        @vote.create(value: -1)
    end 

    describe "first_validation" do
        it "only allows -1 as a value" do
            expect(@vote.first_validation).to eq(-1)
        end
    end

    describe "second_validation" do
        it "only allows 1 as a value" do
            expect(@vote.second_validation).to eq(1)
        end
    end             
end                 

结束

如果您想测试验证,也许可以执行以下操作:

describe "validations" do
  it 'is valid if the value is 1' do
    expect(Vote.new(value: 1)).to be_valid
  end    

  it 'is valid if the value is -1' do
    expect(Vote.new(value: -1)).to be_valid
  end  

  [-3, 0, 4].each do |invalid_value|
    it "is not valid if the value is #{invalid_value}" do
      expect(Vote.new(value: invalid_value)).not_to be_valid
    end
  end       
end  

如果您想测试验证,也许可以执行以下操作:

describe "validations" do
  it 'is valid if the value is 1' do
    expect(Vote.new(value: 1)).to be_valid
  end    

  it 'is valid if the value is -1' do
    expect(Vote.new(value: -1)).to be_valid
  end  

  [-3, 0, 4].each do |invalid_value|
    it "is not valid if the value is #{invalid_value}" do
      expect(Vote.new(value: invalid_value)).not_to be_valid
    end
  end       
end  
阿曼约特

正如萨沙在评论中提到的那样。我想你可以继续下面的代码

require 'rails_helper'

describe Vote do

describe "validations" do

    before do
        @first_vote = Vote.create(value: -1) #  Vote.new(value: -1) - should try this too
        @second_vote= Vote.create(value: 1) # Vote.new(value: 1) -  should try this too
    end 

    describe "first_validation" do
        it "only allows -1 as a value" do
            expect(@first_vote.value).to eq(-1)
        end
    end

    describe "second_validation" do
        it "only allows 1 as a value" do
            expect(@second_vote.value).to eq(1)
        end
    end             
end  
试试这样的东西。您需要对投票模型使用
create
操作。

Amanjot

正如萨沙在评论中提到的那样。我想你可以继续下面的代码

require 'rails_helper'

describe Vote do

describe "validations" do

    before do
        @first_vote = Vote.create(value: -1) #  Vote.new(value: -1) - should try this too
        @second_vote= Vote.create(value: 1) # Vote.new(value: 1) -  should try this too
    end 

    describe "first_validation" do
        it "only allows -1 as a value" do
            expect(@first_vote.value).to eq(-1)
        end
    end

    describe "second_validation" do
        it "only allows 1 as a value" do
            expect(@second_vote.value).to eq(1)
        end
    end             
end  

试试这样的东西。您需要对投票模型使用
create
操作。

您想在这里做什么
@vote=vote.create(:post_id)
总的来说,我正在尝试验证对帖子的投票,仅限于1或-1。如果通过的值不是1和-1,您希望发生什么?应该返回“false”。这里的直接问题是您创建投票的方式。
new
create
方法需要散列,而不仅仅是符号。你需要
{post\u id:something}
。如果你只给它
post\u id
,Rails会从中推断出什么?你想在这里做什么
@vote=vote.create(:post_id)
总的来说,我正在尝试验证对帖子的投票,仅限于1或-1。如果通过的值不是1和-1,您希望发生什么?应该返回“false”。这里的直接问题是您创建投票的方式。
new
create
方法需要散列,而不仅仅是符号。你需要
{post\u id:something}
。如果你只是给它
post\u id
,Rails会从中推断出什么?