我在测试我的rails应用程序时遇到问题。我有我的测试代码:
describe Categoria do
let(:categoria) { FactoryGirl.create(:categoria) }
it { should be_valid }
describe "agregar campos" do
before { @campo = categoria.campos.build(nombre: "campo1", tipo_campo: "check_box") }
it { @campo.should be_valid }
it { @campo.should respond_to :nombre }
it { @campo.should respond_to :tipo_campo }
it { @campo.should respond_to :requerido }
describe "valores válidos" do
valores = %w[texto texto_extenso numero decimal check_box]
valores.each do |valido|
@campo.tipo_campo = valido
@campo.should be_valid
end
end
end
end
如果我在没有块“valoresválidos”的情况下运行测试,则所有其他测试都通过,但是当我运行所有测试时,我收到此错误:
Rack::File headers parameter replaces cache_control after Rack 1.5.
Exception encountered: #<NoMethodError: undefined method `tipo_campo=' for nil:NilClass>
backtrace:
/home/alejo/proyectos/lachiquitienda/spec/models/categoria_spec.rb:38:in `block (4 levels) in <top (required)>'
/home/alejo/proyectos/lachiquitienda/spec/models/categoria_spec.rb:37:in `each'
/home/alejo/proyectos/lachiquitienda/spec/models/categoria_spec.rb:37:in `block (3 levels) in <top (required)>'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/alejo/proyectos/lachiquitienda/spec/models/categoria_spec.rb:35:in `block (2 levels) in <top (required)>'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/alejo/proyectos/lachiquitienda/spec/models/categoria_spec.rb:27:in `block in <top (required)>'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
/home/alejo/proyectos/lachiquitienda/spec/models/categoria_spec.rb:5:in `<top (required)>'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/test_framework/rspec.rb:11:in `run_tests'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
/home/alejo/.rvm/gems/ruby-1.9.3-p374/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
/home/alejo/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/home/alejo/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/home/alejo/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/home/alejo/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/home/alejo/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'
[Finished in 5.9s]
为什么@campo nil iniside每个循环?
提前致谢
答案 0 :(得分:2)
因为当您尝试访问测试时,您不在测试环境中。
所有其他测试都标有it { }
块(rspec也会接受specify { }
块) - 你的循环没有定义任何测试。
尝试以下内容:
describe "valores válidos" do
valores = %w[texto texto_extenso numero decimal check_box]
valores.each do |valido|
it "allows the value #{valido}" do # put your own translation in here!
@campo.tipo_campo = valido
@campo.should be_valid
end
end
end
现在,您的作业在测试范围内,并且将定义变量@campo
。