你好,
我的控制器中有两种方法,如果参数是某个值,我试图让一种方法触发,如果参数不是该值,则触发另一种方法。当参数等于某个值时,我的“not”方法就会触发。
根据(我的理解)这一点,我正确地编写了表达式。以下是这两个方法的签名:
@RequestMapping(value = "/doit", params= {"output=grouped", "display!=1"})
public @ResponseBody HashMap<String, Object> doSomething(
@RequestParam("text") String text,
@RequestParam("display") String display)
{
// this method runs if display=1 but why?
}
@RequestMapping(value = "/doit", params= {"output=grouped", "display=1"})
public @ResponseBody HashMap<String, Object> doSomethingElse(
@RequestParam("text") String text)
{
// this method is not being called when display=1...why not?
}
Run Code Online (Sandbox Code Playgroud)
我启用了 Spring 调试日志记录,并且看到 Spring 将参数转换为值为“1”的 RequestParam 字符串。然而,在下一行中,它决定将其映射到错误的方法。
我究竟做错了什么?
谢谢!
我正在开发 MVP(最小可行产品)。为了提供一种更简单的方法来保护管理页面,我只是将 http_basic_authenticate_with 添加到我的 AdminController 中。
问题是,当我想测试我的 AdminController 时,我因未登录而收到“未经授权”(401)。
在这种情况下,测试身份验证是无关紧要的——它只是暂时的,一旦我进入下一个冲刺,它就会被删除——所以我试图在 RSpec 中跳过它。
问题是我尝试了很多方法,但似乎都不起作用。
例如,我尝试修改http_basic_authenticate_with以避免身份验证。像这样:
require 'spec_helper'
module ActionController
module HttpAuthentication
module Basic
def http_basic_authenticate_with(*args)
end
end
end
end
describe Admin::SubscribersController do
describe "GET 'index'" do
it "should be OK" do
get 'index'
response.should be_successful
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,对于这个简单的测试它仍然返回“false”。
顺便说一句,为了简化这个测试,我的 AdminController 上只有一个空索引操作和一个空视图 (index.html.erb)。
在我的一些控制器中,我有一个没有相应路由的操作,因为它只能通过render ... and return其他控制器操作中的访问。
例如我有一个动作
def no_such_page
# displays a generic error screen
end
Run Code Online (Sandbox Code Playgroud)
在我的 RSpec 控制器测试中,如何“获取”该方法并查看响应主体?
如果我尝试:
get :no_such_page
response.status.should be(200)
Run Code Online (Sandbox Code Playgroud)
它当然给出了错误
No route matches {:controller=>"foo", :action=>"{:action=>:no_such_page}"}
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个会话控制器。现在我想测试用户控制器,只有登录后才能访问该控制器。
我的问题是如何伪造会话。我已经尝试了很多,所以我希望你现在可以帮助我:
应用控制器:
helper_method :current_user
def authenticate_user
current_user
if @current_user
return true
else
redirect_to(:controller => 'sessions', :action => 'new')
return false
end
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
Run Code Online (Sandbox Code Playgroud)
用户控制器具有:
before_filter :authenticate_user
Run Code Online (Sandbox Code Playgroud)
控制器测试:
describe "GET index" do
it "assigns all users as @users" do
user = FactoryGirl.create(:user)
@request.session[:user_id] = user.id
get 'users'
expect(@users.size).to eq(1)
end
end
Run Code Online (Sandbox Code Playgroud)
我也尝试过session[:user_id] = user.id并调试(在获取请求之后):
@current_user总是为零User.find(session[:user_id])返回正确的用户我有一个简单的模型和控制器。我们以猕猴桃为例:
def index
@kiwis = Kiwi.all
end
def destroy
kiwi = Kiwi.find(params[:id])
kiwi.destroy
redirect_to 'index'
end
Run Code Online (Sandbox Code Playgroud)
我在索引页面上使用了删除链接。当我使用 redirect_to 'index' 页面不会刷新模型。我必须在页面上进行硬刷新才能删除 Kiwi。但是,如果我使用 redirect_to action: 'index' 或 redirect_to kiwis_path 页面将在销毁操作后更新。我似乎无法找到对此的解释。任何人都可以对这个问题有所了解。
ruby model-view-controller controller ruby-on-rails ruby-on-rails-4
我正在尝试创建监听“/login”的 à Rest 控制器我已经定义了下面的代码但是当我打开http://localhost:8080/login 时我收到一个 404 错误...请帮忙:)
这是我的包结构:
com.my.package
|_ Application.java
|_ controller
|_ LoginController
Run Code Online (Sandbox Code Playgroud)
我的应用程序:
@ComponentScan("com.my.package.controller")
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的休息控制器:
@RestController
public class LoginController {
@RequestMapping("/login")
public @ResponseBody String getLogin(){
return "{}";
}
}
Run Code Online (Sandbox Code Playgroud) 我使用完美运行的“locals”属性向我的 md-Dialog 发送了一些信息。用户按下按钮后,他将通过 $resource 方法发送一些信息并获得响应。我需要在 md 对话框关闭后显示该响应。如何将该响应发送给我的第一个控制器?
这是示例:
//Main controller
app.controller('Postulation_Ctrl', function($scope, $mdDialog,Postulation, Lista_Complejos){
//md-dialog function
$scope.showPrompt = function(ev){
var parentEl = angular.element(document.body);
var confirm = $mdDialog.show({
parent: parentEl,
locals: {
values: $scope.values,
},
targetEvent: ev,
t templateUrl: 'view/example.html',
controller: function DialogController($scope, $mdDialog, valores, postulaciones,user_id, Postulation) {
$scope.result = values;
$scope.createPostulation = function(){
$scope.postulation = {};
//some logic
auxPostulation = new Postulation($scope.postulation);
auxPostulation.$save(null, function(){
$scope.queryPost();
);
}
$mdDialog.hide();
}
$scope.queryPost = function() {
Postulation.query(function(response){
$scope.postulations = response; <----------I NEED TO …Run Code Online (Sandbox Code Playgroud) 我们如何为 JavaFX 控制器逻辑正确编写单元/集成测试?假设我正在测试的 Controller 类名为LoadController,它的单元测试类是LoadControllerTest,我的困惑源于:
如果LoadControllerTest该类LoadController通过LoadController loadController = new LoadController();I实例化一个新对象,则
可以通过(许多)setter 将值注入控制器。这似乎是不使用反射(遗留代码)的唯一方法。如果我不将值注入 FXML 控件,则控件显然尚未初始化,返回 null。
如果我改用FXMLLoader'sloader.getController()方法来检索loadController它,它将正确初始化 FXML 控件,但initialize()因此调用了控制器的方法,这会导致运行非常缓慢,并且由于无法注入模拟依赖项,因此它更像是一个写得不好的集成测试.
我现在正在使用前一种方法,但有更好的方法吗?
测试效果
这里的答案涉及 TestFX,它@Tests基于主应用程序的start方法而不是Controller 类。它显示了一种测试控制器的方法
verifyThat("#email", hasText("test@gmail.com"));
Run Code Online (Sandbox Code Playgroud)
但是这个答案涉及DataFX - 而我只是询问 JavaFX 的 MVC 模式。大多数 TestFX 讨论都集中在它的 GUI 功能上,所以我很好奇它是否也适合控制器。
以下示例显示了我如何使用 a 注入控制器,VBox以便在测试期间它不为空。有没有更好的办法?请具体点
public class LoadControllerTest {
@Rule
public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();
private LoadController …Run Code Online (Sandbox Code Playgroud) 我注意到是否使用此属性是一样的。我错了吗?
举个例子:
[Route("[controller]")]
[ApiController]
public class DataTablesController: ControllerBase
{
[HttpGet]
public IActionResult Test()
{
return Ok("test");
}
}
Run Code Online (Sandbox Code Playgroud)
当我删除该[ApiController]属性时什么也没发生。
在微软的文档中,我找到了这样的解释:
指示类型和所有派生类型用于提供 HTTP API 响应。
使用此属性修饰的控制器配置了旨在改善开发人员构建 API 体验的功能和行为。
在程序集中装饰时,程序集中的所有控制器都将被视为具有 API 行为的控制器。
什么是 API 行为?我们为什么要使用它?
c# controller asp.net-apicontroller .net-attributes asp.net-core
public function fetchdrug(Request $search_drug){
$filter_drug = $search_drug->input('search_drug');
$all_drugs = HmsBbrKnowledgebaseDrug::selectRaw('DISTINCT ON (drug_code)
drug_code,
drug_name,
JSON_AGG(drug_dosage) AS dosage_list')
->GroupBy('drug_code', 'drug_name')
->orderBy('drug_code', 'ASC')
->get();
return response()->json([
'all_drugs'=>$all_drugs,
]);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用JSON_AGG检索多行drug_dosage并将它们合并为一行,但是我的输出中出现了括号和引号,我该如何将其取出?
更新:我在示例中遇到错误,因为我正在尝试使用str_replace和 的解决方案preg_replace。我的问题是目标在 SQL 语句中,所以我怀疑这与错误有关,因为结果错误中还有其他数据:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in
{"drug_code":"CFZU",
"drug_name":"Cefazolin",
"dosage_list":"[\"<=4 mg\/L\", \"<=3 mg\/L\"]"},
{"drug_code":"TZPD","drug_name":"Pip\/Tazobactam",
"dosage_list":"[\"Pip\/Tazobactam\"]"}
Run Code Online (Sandbox Code Playgroud) controller ×10
rspec ×3
java ×2
ruby ×2
spring ×2
angularjs ×1
asp.net-core ×1
c# ×1
function ×1
javafx ×1
laravel ×1
mddialog ×1
rest ×1
session ×1
spring-mvc ×1
sql ×1
testfx ×1
testing ×1
unit-testing ×1