RSpec:我如何将模块混合到RSpec测试中?

时间:2022-02-09 20:58:17

In my app when user share something he's rating grows. When he tries to share something twice – he will get no additional rating for second try. For application, share callback is triggered by client-side with JS, so, it's just a regular GET-request. So, I need to test this functionality. It's easy. But I'v got several sections with this behavior. Every controller from that sections have method named "rating_from_share", so tests are pretty similar. I think it is good idea to extract that test's in a mixing and include them where it should be, but I can't figure out, how can I do this. So, is it real to include a mixing with RSpec to a RSpec test? Maybe something kind of metaprogramming can solve this problem?

在我的应用程序中,当用户分享他的评级增长的东西。当他试图分享两次东西时 - 他会在第二次尝试时没有获得额外的评分。对于应用程序,共享回调是由客户端用JS触发的,因此,它只是一个常规的GET请求。所以,我需要测试这个功能。这很容易。但我有几个部分有这种行为。该部分的每个控制器都有名为“rating_from_share”的方法,因此测试非常相似。我认为最好在混合中提取测试并将它们包含在应有的位置,但我无法弄清楚,我该怎么做。那么,将RSpec与RSpec混合测试包含在内是真的吗?也许某种元编程可以解决这个问题?

P.S. realization of "rating_from_share" method is not really the same but only the output result, so I can't to aggregate it to a superclass and test them here.

附: “rating_from_share”方法的实现并不是真的相同而只是输出结果,所以我不能将它聚合到一个超类并在这里测试它们。

EDIT:

According to Vimsha answer, should I do something like this?

根据Vimsha的回答,我应该这样做吗?

Module Share
  def share
    it 'should be fun'
      expect(@fun.isFun?).toBe == 'yup' # the @fun is declared in ShareTest
    end
  end
end

describe "Share Test" do
   extend Share
   before :each do
     @fun = Fun.new
   end
   it 'should do test' do
     share # call method from Share module, which has real RSpec code?
   end
end

The code is written just here, I'm just trying to get the idea.

代码就是在这里编写的,我只想尝试一下。

3 个解决方案

#1


1  

module Share
  def share
  end
end

describe "Share Test" do
   extend Share
end

You can call the methods of the module directly within the tests

您可以在测试中直接调用模块的方法

#2


4  

You can use shared examples.

您可以使用共享示例。

These are typically saved under spec/support and loaded via spec_helper.rb. Be sure to read the docs to understand how to load the shared code--it is not automagically performed for you.

这些通常保存在spec / support下并通过spec_helper.rb加载。请务必阅读文档以了解如何加载共享代码 - 它不会自动执行。

Once they are defined you can include them like so:

一旦定义它们,您可以像这样包含它们:

# spec/support/decorated_model.rb
shared_examples "decorated_model" do
  it "can be decorated" do
    subject.should respond_to?(:decorate)
  end
end

# my_class_spec.rb
describe MyClass do
  it_behaves_like "decorated_model"
end

#3


3  

A common practice in RSpec is to store such logic under spec/support. For instance:

RSpec的一个常见做法是将此类逻辑存储在spec / support下。例如:

# spec/support/ratings_macros.rb
module RatingsMacros
  ...
end

You then need to load it from your spec_helper:

然后,您需要从spec_helper加载它:

# spec/spec_helper.rb
...
RSpec.configure do |config|
  ...
  config.include RatingsMacros

You can now call in your tests all the methods defined in the RatingsMacros module.

您现在可以在测试中调用RatingsMacros模块中定义的所有方法。

#1


1  

module Share
  def share
  end
end

describe "Share Test" do
   extend Share
end

You can call the methods of the module directly within the tests

您可以在测试中直接调用模块的方法

#2


4  

You can use shared examples.

您可以使用共享示例。

These are typically saved under spec/support and loaded via spec_helper.rb. Be sure to read the docs to understand how to load the shared code--it is not automagically performed for you.

这些通常保存在spec / support下并通过spec_helper.rb加载。请务必阅读文档以了解如何加载共享代码 - 它不会自动执行。

Once they are defined you can include them like so:

一旦定义它们,您可以像这样包含它们:

# spec/support/decorated_model.rb
shared_examples "decorated_model" do
  it "can be decorated" do
    subject.should respond_to?(:decorate)
  end
end

# my_class_spec.rb
describe MyClass do
  it_behaves_like "decorated_model"
end

#3


3  

A common practice in RSpec is to store such logic under spec/support. For instance:

RSpec的一个常见做法是将此类逻辑存储在spec / support下。例如:

# spec/support/ratings_macros.rb
module RatingsMacros
  ...
end

You then need to load it from your spec_helper:

然后,您需要从spec_helper加载它:

# spec/spec_helper.rb
...
RSpec.configure do |config|
  ...
  config.include RatingsMacros

You can now call in your tests all the methods defined in the RatingsMacros module.

您现在可以在测试中调用RatingsMacros模块中定义的所有方法。