[英]Cleaning up shared_context variables in rspec
I am using RSpec.shared_context
to set variables that all the describe blocks will use.我正在使用
RSpec.shared_context
来设置所有描述块将使用的变量。
Something like this像这样的东西
RSpec.shared_context "common" do
let(:name) { #creates a database object }
#more let statements
end
Now I invoke it from describe block like so现在我像这样从描述块中调用它
describe "common test" do
include_context "common"
#run few tests
end
Now after running the describe block I want to clean it up.现在运行描述块后,我想清理它。 How do I rollback all the objects created in the shared context?
如何回滚在共享上下文中创建的所有对象?
I tried cleaning it in the after(:context)
hook but since it is a let statement the variable name
is only allowed inside examples.我尝试在
after(:context)
钩子中清理它,但由于它是一个 let 语句,因此变量name
只允许在示例中使用。
Is there someway I can use use_transactional_fixtures
to clean this up after running the tests in the describe block.在描述块中运行测试后,我是否可以使用
use_transactional_fixtures
来清理它。
You don't need to worry about cleaning up your "lets" if you just setup your test suite properly to wipe the database.如果您只是正确设置测试套件以擦除数据库,则无需担心清理“let”。
Use let to define a memoized helper method.
使用 let 定义一个记忆化的辅助方法。 The value will be cached across multiple calls in the same example but not across examples.
该值将在同一示例中的多个调用中缓存,但不会跨示例缓存。
Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked.
注意 let 是惰性求值的:直到它定义的方法第一次被调用时才会求值。
In almost every case you want teardown to happen automatically and per example.几乎在每种情况下,您都希望自动进行拆卸,例如。 Thats what
config.transactional_fixtures
does - it rolls back the database after every example so that you have a fresh slate and don't get test ordering issues.这就是
config.transactional_fixtures
所做的 - 它在每个示例之后回滚数据库,这样您就有了新的石板并且不会遇到测试排序问题。 Relying on each example / context whatever to explicitly clean up after itself is just a recipe for failure.依靠每个示例/上下文来显式清理自身只是失败的秘诀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.