【问题标题】:Is it possible to redefine "describe"?是否可以重新定义“描述”?
【发布时间】:2013-04-11 18:34:34
【问题描述】:

有什么方法可以重新定义describe,例如,我可以写

new_describe MyModule::MyClass do
  it "does this" do
  end
  it "does that" do
  end
end

而不是

describe "something" do
  def app
    MyModule::MyClass
  end
  it "does this" do
  end
  it "does that" do
  end
end

?

【问题讨论】:

  • 你到底想做什么?也许还有另一种方法。

标签: ruby rspec dsl


【解决方案1】:

您已经可以使用可用的语法描述一个特定的类:

describe MyModule::MyClass do
  it "is available as described_class" do
    described_class.should eq(MyModule::MyClass)
  end
end

更多:Rspec

【讨论】:

  • 谢谢@fmendez,但这并不是我想要的……我正在寻找一种将def app (...) end 指令“注入”到块中的方法。
【解决方案2】:

我想知道this blog post by David Chelimsky 是否在谈论您要做什么?如果是这样:

describe MyModule::MyClass do
  subject(:app){ MyModule::MyClass.new }
  it { should_not be_nil }
  it "does that" do
    app.that == "that"
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2016-11-08
    相关资源
    最近更新 更多