Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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 如何对名称空间模型进行单元测试_Ruby On Rails_Unit Testing_Namespaces_Shoulda - Fatal编程技术网

Ruby on rails 如何对名称空间模型进行单元测试

Ruby on rails 如何对名称空间模型进行单元测试,ruby-on-rails,unit-testing,namespaces,shoulda,Ruby On Rails,Unit Testing,Namespaces,Shoulda,在我的Rails应用程序中,我大量使用了模型目录的子目录,因此也大量使用了命名空间模型——也就是说,我有以下目录继承权: models | +- base_game | | | +- foo.rb (defines class BaseGame::Foo) | +- expansion | +- bar.rb (defines class Expansion::Bar) 这是由Rails强制实现的——也就是说,调用类“Just”Foo和Bar是行不通的 我想对这些类进行单元测

在我的Rails应用程序中,我大量使用了模型目录的子目录,因此也大量使用了命名空间模型——也就是说,我有以下目录继承权:

models
|
+- base_game
|  |
|  +- foo.rb (defines class BaseGame::Foo)
|
+- expansion
   |
   +- bar.rb (defines class Expansion::Bar) 
这是由Rails强制实现的——也就是说,调用类“Just”
Foo
Bar
是行不通的

我想对这些类进行单元测试(在ActiveSupport::TestCase之上使用Shoulda)。我知道被测试的类的名称将从测试用例类的名称派生


如何编写单元测试来测试这些类?

这应该可以直接工作,我使用Rspec,这很简单:

describe User::Profile do

  it "should exist" do
    User::Profile.class.should be_a Class
  end

  it { should validate_presence_of(:name) }

end

事实证明,即使在正常的
ActiveSupport
/
Test::Unit
情况下,它也是直接的。只需复制
/test
下的模型目录结构即可:

test
|
+- base_game
|  |
|  +- foo_test.rb (defines class BaseGame::FooTest)
|
+- expansion
   |
   +- bar_test.rb (defines class Expansion::BarTest) 

下面是一些对我来说既适用于
ruby-I
又适用于
zeus
的代码:

require "test_helper"

class BaseGame::Foo < ActiveSupport::TestCase
  include BaseGame

  should "let #baz do something" do
    assert Foo.new.baz
  end

end
需要“测试助手”
类BaseGame::Foo
你也可以

require "test_helper"

class BaseGame::Foo < ActiveSupport::TestCase

  should "let #baz do something" do
    assert BaseGame::Foo.new.baz
  end

end
需要“测试助手”
类BaseGame::Foo