Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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 Rspec测试:它{应该响应_to()}_Ruby_Unit Testing_Rspec2 - Fatal编程技术网

Ruby Rspec测试:它{应该响应_to()}

Ruby Rspec测试:它{应该响应_to()},ruby,unit-testing,rspec2,Ruby,Unit Testing,Rspec2,如果您有一个正在测试其存在性的属性列表,并且您有以下内容: before do @attr = Employee.new(name: "One", email: "onetwo@three.com") end subject { @attr } it { should respond_to(:name) } it { should respond_to(:email) } it { should be_valid } 然后,是否可以使用以

如果您有一个正在测试其存在性的属性列表,并且您有以下内容:

before do
  @attr = Employee.new(name: "One",
                       email: "onetwo@three.com")
end

subject { @attr }

it { should respond_to(:name) }
it { should respond_to(:email) }

it { should be_valid }
然后,是否可以使用以下方法测试相反的属性,即这些属性是否为空:

it { should_not respond_to(:reverse, :blank) }
it { should_not be_valid }
我确实试过上面的方法,但我并不完全理解这些问题,即使是在阅读之后


有人知道如何使用相同的初始化来测试是否存在,然后转身测试空白属性吗?

您对用户对象的单独测试应该更像这样:

it "should have an email attribute" do
  @attr.should respond_to(:email) }
end
但是,如果您正在测试一个类,我是否可以为您的测试提出一些更类似于此的建议:

before (:each) do
  @attr = {name: "One", email: "onetwo@three.com"}
end
然后,您的第一个测试确保创建工作如下:

it "should create a new instance given valid attributes" do
  Employee.create!(@attr)
end
一旦您确定该测试有效,您可以继续进行其他测试,并自动创建员工,如下所示:

describe "Attributes and method tests" do
  before (:each) do
    @employee = Employee.create(@attr)
  end

  it "should have a name attribute" do
    @employee.should respond_to(:name)
  end

  it "should have an email attribute" do
    @employee.should respond_to(:email) }
  end

  # etc... (to test other attributes or methods)
end
为了回答您关于反转测试并指定如果属性为空则测试无效的问题,我将尝试以下方法。如果您有在场验证,这些测试将很好:

it "should not be valid with a blank email" do
  Employee.new(@attr.merge(:email => '').should_not be_valid
end

it "should not be valid with a blank name" do
  Employee.new(@attr.merge(:name => '').should_not be_valid
end

我想你误解了
在测试中的反应。当你这样做的时候

it { should respond_to(:name) }
您断言
@attr.response\u to(:name)
返回
true
,这意味着该方法存在,而不是返回值。如果要测试是否有返回的值,可以执行以下操作:

its(:name){ should be_present }
这将确保调用
name
方法的结果返回一个非false、非空值。然后,当你反向测试时

its(:name){ should_not be_present }

您断言
name
方法返回空白。您必须为第二次测试创建不同的设置,因为您正在以不同的状态测试对象。

这是主观的,根本无法回答问题。很抱歉,我一开始误读了您的问题。我以为你说你很难理解写测试的正确格式。我已经更新了我的答案。好吧,这不是我的问题,但在我看来,把你原来的东西留在里面仍然是误导性的主观失误。从他提问的措辞来看,他似乎是一个没有经验的rspec用户,所以我试图提供帮助并给出一些建议。是的,我的模型中确实存在验证。。。你指出这一点很有帮助。谢谢。你到底想在这里测试什么?测试某个方法是否响应以及测试该方法的返回值是否为空都是不相交的概念。谢谢@Emily。。。我确实对你的回答有错误的理解。我需要做更多的阅读和实验来得到你的指导。当涉及到是否有价值时,验证:存在是否符合标准?Rspec的伟大之处在于,自己测试这类东西是多么容易。当您在Rspec中调用
be\u present
时,它正在模型上调用
present?
。与
respond\u to
调用
respond\u to?
相同。然后,您可以在IRB中轻松地测试这些方法。但是,要直接回答,
present?
返回
true
nil
false
[]
{}
以外的任何内容。