rspec-rails和使用let导致变量冲突?
正在编写一些测试并遇到使用let
来定义一些变量的问题。
我花了一些时间制作一个最小的失败例子:
require "spec_helper" describe "Some behavior" do let(:attachments_dir) { "some_test_dir" } before :all do attachments_dir # Commenting out this line makes the tests pass end context "nested 1" do let(:api_params) do { message: { to: "recipient1", from: "sender1"} } end before do puts "nested 1 before " + api_params.to_s end it "nested 1 example 1" do api_params[:message][:to].should == "recipient1" end end context "nested 2" do let(:api_params) do { message: { from: "sender2"} } end before do puts "nested 2 before " + api_params.to_s end it "nested 2 example 1" do api_params[:message][:to].should be_nil end end end
此测试失败
nested 1 before {:message=>{:to=>"recipient1", :from=>"sender1"}} nested 2 before {:message=>{:to=>"recipient1", :from=>"sender1"}} expected: nil got: "recipient1"
但是,注释掉该示例的第8行会导致测试通过
#attachments_dir
为什么顶级let
干扰嵌套上下文let
? 这对我来说就像一个RSpec错误,但我想在发布到他们的问题列表之前我会问这里。
- Rails载波测试 – 如何在测试后删除文件?
- 对模型属性进行存根
- 使用Capybara下载CSV
- Rails夹具未加载rspec
- 在某些RSpec rails请求示例中测试HTTP状态代码,但是对于其他RSPro rails请求示例
- 在RSpec / Rails验收测试中,有没有办法将消息推送到RSpec输出并保持良好的缩进?
- rail3 / rspec / devise:除非我添加一个dummy = subject.current_user.inspect,否则rspec控制器测试失败
- 测试不通过:未定义的方法`authenticate!’ 为零:NilClass?
- 如何使用RSpec测试Rails 4应用程序并从Railscast#53(修订版)中自定义exception处理?