【问题标题】:Rspec match syntaxRspec 匹配语法
【发布时间】:2013-11-29 09:09:38
【问题描述】:

第一个测试通过,但其他两个没有。我的语法有什么问题?唯一的区别是 matcheq。我知道我以前用过match,但我似乎无法在网上找到它的好文档。

我得到的错误是:undefined method 'match' for 1:Fixnum

describe Die do
    describe "new roll" do #TEST PASSES
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to eq 1
        end
    end

    describe "new roll" do # error undefined method 'match' for 1:Fixnum
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to match 1
        end
    end

    describe "new roll" do # error expected /\d/ got 1
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to eq (/\d/)
        end
    end
end

【问题讨论】:

  • match matcher 在对象上调用#match,它适用于字符串。

标签: ruby-on-rails ruby regex rspec


【解决方案1】:

您的第二个测试没有通过,因为match 用于将字符串或正则表达式与字符串进行比较,而不是 Fixnums。您的第三个测试没有通过,因为正则表达式 /\d/等于 到 Fixnum 1,它匹配字符串 "1"

describe Die do
    describe "new roll" do #TEST PASSES
        it "returns a number" do
            expect(Die.instance_method(:initialize).arity).to eq(1)
        end
    end

    describe "new roll" do
        it "returns a number" do
            # 1.to_s equals the String "1" ...
            expect(Die.instance_method(:initialize).arity.to_s).to eq("1")
        end
    end

    describe "new roll" do
        it "returns a number" do
            # ... and "1" matches the regex /\d/
            expect(Die.instance_method(:initialize).arity.to_s).to match(/\d/)
        end
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2015-12-17
    相关资源
    最近更新 更多