在 before(:all) 中动态添加方法
Dynamically add method in before(:all)
我正在测试可以包含在控制器中的模块。
现在代码看起来像这样:
class GithubAuthorizationController < ApplicationController
include Frontend::Concerns::GithubAuthorization
def index
render inline: "lorem_ipsum"
end
end
describe GithubAuthorizationController do
before(:all) do
@page_content = "lorem_ipsum"
end
...........
如您所见,我基本上是在测试 运行 之前创建了一个测试控制器。现在我想在 before(:all)
块中添加模块和索引方法。我试过了:
class GithubAuthorizationController < ApplicationController
end
describe GithubAuthorizationController do
before(:all) do
@page_content = "lorem_ipsum"
class < @controller
include Frontend::Concerns::GithubAuthorization
def index
render inline: "lorem_ipsum"
end
end
end
...........
正如我在 before(:all)
块中的调试器中看到的那样,@controller
被定义为 <GithubAuthorizationController ...
。所以它是一个实例。 运行ning 代码时也没有错误,但测试失败,因为 The action 'index' could not be found ...
我做错了什么?如何将代码移动到 before(:all)
块?谢谢
在 rspec 中执行此操作的方法是使用控制器块:
describe GithubAuthorizationController, type: :controller do
context "with module" do
controller do
include Frontend::Concerns::GithubAuthorization
def index
render inline: "lorem_ipsum"
end
end
# within this block the controller will be the anonymous controller class created above
end
end
如果您已将 infer_base_class_for_anonymous_controllers
设置为 false(这不是默认设置),那么您需要执行 controller(GithubAuthorizationController)
否则您将直接继承自 ApplicationController
您的问题可能是由于缺少路由 - controller
助手为您创建了一些路由(用于默认索引、显示等)操作。您可以在
示例中添加额外的
it "does something with a custom route" do
routes.draw { get "custom" => "github_authorization#custom" }
get :custom
...
end
我正在测试可以包含在控制器中的模块。 现在代码看起来像这样:
class GithubAuthorizationController < ApplicationController
include Frontend::Concerns::GithubAuthorization
def index
render inline: "lorem_ipsum"
end
end
describe GithubAuthorizationController do
before(:all) do
@page_content = "lorem_ipsum"
end
...........
如您所见,我基本上是在测试 运行 之前创建了一个测试控制器。现在我想在 before(:all)
块中添加模块和索引方法。我试过了:
class GithubAuthorizationController < ApplicationController
end
describe GithubAuthorizationController do
before(:all) do
@page_content = "lorem_ipsum"
class < @controller
include Frontend::Concerns::GithubAuthorization
def index
render inline: "lorem_ipsum"
end
end
end
...........
正如我在 before(:all)
块中的调试器中看到的那样,@controller
被定义为 <GithubAuthorizationController ...
。所以它是一个实例。 运行ning 代码时也没有错误,但测试失败,因为 The action 'index' could not be found ...
我做错了什么?如何将代码移动到 before(:all)
块?谢谢
在 rspec 中执行此操作的方法是使用控制器块:
describe GithubAuthorizationController, type: :controller do
context "with module" do
controller do
include Frontend::Concerns::GithubAuthorization
def index
render inline: "lorem_ipsum"
end
end
# within this block the controller will be the anonymous controller class created above
end
end
如果您已将 infer_base_class_for_anonymous_controllers
设置为 false(这不是默认设置),那么您需要执行 controller(GithubAuthorizationController)
否则您将直接继承自 ApplicationController
您的问题可能是由于缺少路由 - controller
助手为您创建了一些路由(用于默认索引、显示等)操作。您可以在
it "does something with a custom route" do
routes.draw { get "custom" => "github_authorization#custom" }
get :custom
...
end