Ruby 如何参数化RSpec测试,以便在稍微不同的条件下测试相同的行为
我正在实施一项服务,该服务有几种不同的访问方式:Ruby 如何参数化RSpec测试,以便在稍微不同的条件下测试相同的行为,ruby,rspec,Ruby,Rspec,我正在实施一项服务,该服务有几种不同的访问方式: 使用简单的查询参数 使用编码为Javascript对象的参数 对于某些调用,GET和POST都受支持,在向服务发送大量数据时使用POST 构造RSpec测试的最佳方法是什么,以避免不必要地重复代码,从而允许我每次运行相同的基本断言 我已经在使用shared_示例捕获一些注释测试,比如响应代码、mimetype等。但我想知道是否还有其他选项,特别是当我想使用所有请求方法和一系列预期的输入和输出来调用服务时。在这种情况下,我的方法是将请求指定为执
- 使用简单的查询参数
- 使用编码为Javascript对象的参数
我已经在使用shared_示例捕获一些注释测试,比如响应代码、mimetype等。但我想知道是否还有其他选项,特别是当我想使用所有请求方法和一系列预期的输入和输出来调用服务时。在这种情况下,我的方法是将请求指定为执行它的lambda。这样我就可以在我的共享规范中引用它,并为每种类型的请求设置不同的规范 我喜欢在设置期望时使用rspec descripe块,在这种情况下使用特定的请求方法。整个事情看起来是这样的:
describe FooController do
shared_examples_for "any request" do
it "assigns foo" do
@request.call
assigns[:foo].should == "bar"
end
it "does not change the number of bars" do
@request.should_not change(Bar, :count)
end
end
context "using GET" do
before do
@request = lambda { get "index" }
end
it_should_behave_like "any request"
end
end
一种更简洁的方法是使用“let”构造,尽管对于新手来说,这可能是rSpec魔术中的一个太深的步骤:
describe FooController do
shared_examples_for "any request" do
it "assigns foo" do
request.call
assigns[:foo].should == "bar"
end
it "does not change the number of bars" do
request.should_not change(Bar, :count)
end
end
context "using GET" do
let(:request) { lambda { get "index" } }
it_should_behave_like "any request"
end
end
在本例中,我的方法是将请求指定为执行它的lambda。这样我就可以在我的共享规范中引用它,并为每种类型的请求设置不同的规范 我喜欢在设置期望时使用rspec descripe块,在这种情况下使用特定的请求方法。整个事情看起来是这样的:
describe FooController do
shared_examples_for "any request" do
it "assigns foo" do
@request.call
assigns[:foo].should == "bar"
end
it "does not change the number of bars" do
@request.should_not change(Bar, :count)
end
end
context "using GET" do
before do
@request = lambda { get "index" }
end
it_should_behave_like "any request"
end
end
一种更简洁的方法是使用“let”构造,尽管对于新手来说,这可能是rSpec魔术中的一个太深的步骤:
describe FooController do
shared_examples_for "any request" do
it "assigns foo" do
request.call
assigns[:foo].should == "bar"
end
it "does not change the number of bars" do
request.should_not change(Bar, :count)
end
end
context "using GET" do
let(:request) { lambda { get "index" } }
it_should_behave_like "any request"
end
end
如果相关,我写的代码在这里:如果相关,我写的代码在这里: