Ruby on rails 重构最后相关的规范代码
我正在使用RubyonRails3.1.0和RSpecRails2gem。我想在my spec文件中重构以下示例代码: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
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
一样,似乎没有在该会话
中保留值。我在
之前的块中初始化了会话,然后在
块的共享示例中使用了会话。您是否在使用控制器。会话
?