Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 如何在rails 3和rspec中的before块中使用@valid_属性?_Ruby On Rails 3_Rspec_Rspec Rails - Fatal编程技术网

Ruby on rails 3 如何在rails 3和rspec中的before块中使用@valid_属性?

Ruby on rails 3 如何在rails 3和rspec中的before块中使用@valid_属性?,ruby-on-rails-3,rspec,rspec-rails,Ruby On Rails 3,Rspec,Rspec Rails,我正在尝试不同的博客,以Rails3和RSpec为例。是的,它在Windows上,所以答案不是不使用Windows。别无选择。继续 我可以使用rspec spec或rake spec:models运行规范,所以看起来不错。但是,如果我尝试使用带有属性的before块,它将无法创建带有这些属性的Person类。其他测试只是为了显示spec可以运行 制作了一个人模型,然后更新了规格 \myapp\spec\models\person\u spec.rb require 'spec_helper'

我正在尝试不同的博客,以Rails3和RSpec为例。是的,它在Windows上,所以答案不是不使用Windows。别无选择。继续

我可以使用rspec spec或rake spec:models运行规范,所以看起来不错。但是,如果我尝试使用带有属性的before块,它将无法创建带有这些属性的Person类。其他测试只是为了显示spec可以运行

制作了一个人模型,然后更新了规格

\myapp\spec\models\person\u spec.rb

require 'spec_helper'

describe Person do

  before(:each) do
    @valid_attributes = {
      :first_name => "Foo", 
      :last_name => "Bar"
    }
  end

  it "should create a new instance given valid attributes" do
    Person.create!(@valid_attributes)
  end

  it "can be instantiated" do
    Person.new.should be_an_instance_of(Person)
  end

  it "can be saved successfully" do
    Person.create.should be_persisted
  end



  #pending "add some examples to (or delete) #{__FILE__}"
end
以下是rake spec:models命令的输出

C:\Users\laptop\Documents\Sites\myapp>rake spec:models
C:/Ruby193/bin/ruby.exe -S rspec ./spec/models/person_spec.rb

Person
←[31m  should create a new instance given valid attributes (FAILED - 1)←[0m
←[32m  can be instantiated←[0m
←[32m  can be saved successfully←[0m

Failures:

  1) Person should create a new instance given valid attributes
     ←[31mFailure/Error:←[0m ←[31mPerson.create!(@valid_attributes)←[0m
     ←[31mActiveRecord::UnknownAttributeError:←[0m
       ←[31munknown attribute: first_name←[0m
←[36m     # ./spec/models/person_spec.rb:13:in `block (2 levels) in <top (required)>'←[0m

Finished in 0.074 seconds
←[31m3 examples, 1 failure←[0m

Failed examples:

←[31mrspec ./spec/models/person_spec.rb:12←[0m ←[36m# Person should create a new instance given valid attributes←[0m
rake aborted!
C:/Ruby193/bin/ruby.exe -S rspec ./spec/models/person_spec.rb failed
C:\Users\laptop\Documents\Sites\myapp>rake规范:模型
C:/Ruby193/bin/ruby.exe-S rspec./spec/models/person\u spec.rb
人
←[31m应在给定有效属性的情况下创建新实例(失败-1)←[0m
←[32m可以实例化←[0m
←[32m可以成功保存←[0m
失败:
1) 人员应在给定有效属性的情况下创建新实例
←[31M故障/错误:←[0m←[31mPerson.create!(@valid_属性)←[0m
←[31mActiveRecord::未知属性错误:←[0m
←[31munknown属性:名字←[0m
←[36m./规格/型号/人员规格rb:13:in‘block(2层)in’←[0m
在0.074秒内完成
←[31m3示例,1个故障←[0m
失败的示例:
←[31mrspec./spec/models/person_spec.rb:12←[0m←[36m#在给定有效属性的情况下,个人应创建一个新实例←[0m
雷克流产了!
C:/Ruby193/bin/ruby.exe-S rspec./spec/models/person\u spec.rb失败
因此,三分之二的人通过了考试,但没有通过具有属性的考试

运行before块需要设置什么特别的内容,或者Rails 3的测试中属性是如何通过的

还有没有一种方法可以消除每一规格行的]31m和类似的打印输出


感谢

从错误中可以看出,ActiveRecord找不到作为@valid\u attributes一部分传递的属性:first\u name。也就是说,问题不在于如何使用RSpec,而在于您希望有效模型包含的属性


检查人名模型上是否有:first\u name字段或属性,并验证拼写是否准确(:first\u name vs:firstname或其他变体)

我应该用答案更新此字段

Person模型实际上包含first_name和last_name,但正如上面两个人所指出的,我收到的错误指向ActiveRecord没有找到它

在Windows中,运行rake db:migrate两到三次最终修复了它,即使它在模型中没有丢失

如果你被困在Windows开发中,这可能是一件好事

我终于能够将Lubuntu放在Windows7上的VirtualBox上,它运行得很好,从那以后,我就开始使用其他示例


干杯

使用rails 3.2.0.rc1、rspec rails 2.8.1、rspec rails 2.8.0人名模型是否包含名字属性?哦,尝试使用--no-color运行。或者使用
ansicon