Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 重构最后相关的规范代码_Ruby On Rails_Ruby_Ruby On Rails 3_Rspec_Rspec2 - Fatal编程技术网

Ruby on rails 重构最后相关的规范代码

Ruby on rails 重构最后相关的规范代码,ruby-on-rails,ruby,ruby-on-rails-3,rspec,rspec2,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,Rspec2,我正在使用RubyonRails3.1.0和RSpecRails2gem。我想在my spec文件中重构以下示例代码: describe "Making things" do it "should make a thing" do # Make the thing ... # This is the same statement as that present in the "should make another # thing" example (rea

我正在使用RubyonRails3.1.0和RSpecRails2gem。我想在my spec文件中重构以下示例代码:

describe "Making things" do
  it "should make a thing" do
    # Make the thing
    ...

    # This is the same statement as that present in the "should make another
    # thing" example (read below for more information)
    response.body.should include("Hello World")
  end

  it "should make another thing" do
    # Make another thing
    ...

    # The same statement as that present in the "should make a thing" example
    response.body.should include("Hello World")
  end
end

如何重构上面的
response.body.should应该包含(“Hello World”)
代码,以便编写更少的代码?也就是说,如何通过使用一条对两个规范示例都有效的语句来测试
response.body
内容?

使用
共享示例

像这样:

describe "Making things" do
  before do
    @user.new
  end

  shared_examples_for "normal case" do
    it "shows hello world" do
      response.body.should include("Hello World")
    end

    # more tests could be here

  end

  context "making a thing" do
    before(:each) do
      # make thing
    end
    it_should_behave_like_a "normal case"          
  end

  context "making another thing" do
    before(:each) do
      # make another thing
    end
    it_should_behave_like_a "normal case"          
  end
end

请参阅文档。

谢谢。现在,我正在对RoR
会话
数据尝试相同的方法,但是
it\u的行为应该像\u a
一样,似乎没有在该
会话
中保留值。我在
之前的
块中初始化了会话,然后在
块的
共享示例中使用了会话。您是否在使用
控制器。会话