我现在正在为控制器制作 Rspec SearchController。该控制器使用通过“get”请求获取的参数通过sql搜索记录。控制器如下。
class SearchController < ApplicationController
def index
if params[:category] == 'All'
@search_result = Item.find_by_sql("SELECT * FROM items WHERE name LIKE '%# {params[:name]}%'")
else
@search_result = Item.find_by_sql("SELECT * FROM items WHERE name LIKE '%#{params[:name]}%' AND category = '#{params[:category]}'")
end
if @search_result.empty? then
redirect_to main_app.root_path, notice: "No items found matching your search criteria ! Modify your search."
else
@search_result=@search_result.paginate(:page => params[:page],:per_page => 3)
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后我编写了简单的 Rspec 测试,如下所示。该测试的目的是首先item使该对象在控制器中使用。Item.stub(:find_by_sql).and_return(item)还声明了存根 ( )。然后get :index用参数做:category …
在经过RSpec测试的Rails应用程序中,我正在测试一个调用.dup()某些记录并保存重复项的函数。如何测试新创建的记录是否与预期的原始记录重复?
我怀疑是否有针对此特定案例的主张。而且我始终可以确保重复记录和原始记录的属性值相同,但记录不同。我想知道这种测试是否有一种更优雅,可以接受的模式。
我跑步后确实收到这些警告rspec
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:259: warning: already initialized constant ERB::Revision
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/erb-2.2.3/lib/erb.rb:260: warning: previous definition of Revision was here
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:367: warning: already initialized constant ERB::Compiler::Scanner::DEFAULT_STAGS
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/erb-2.2.3/lib/erb.rb:369: warning: previous definition of DEFAULT_STAGS was here
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:368: warning: already initialized constant ERB::Compiler::Scanner::DEFAULT_ETAGS
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/erb-2.2.3/lib/erb.rb:370: warning: previous definition of DEFAULT_ETAGS was here
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:489: warning: already initialized constant ERB::Compiler::TrimScanner::ERB_STAG
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/erb-2.2.3/lib/erb.rb:491: warning: previous definition of ERB_STAG was here
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/3.0.0/erb.rb:830: warning: already initialized constant ERB::NOT_GIVEN
/Users/myname/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/erb-2.2.3/lib/erb.rb:832: warning: previous definition of NOT_GIVEN was here
.......
Run Code Online (Sandbox Code Playgroud)
它说我的3.0.0和ruby/3.0.0文件有重复。我想如何删除这个警告?
我正在尝试使用rspec测试我的cancancan能力
但与测试特定用户可以做什么相反,我试图测试用户不应该做什么.
现在,我有一个像这样的上下文块:
context "for a manager" do
before do
@manager = FactoryGirl.build(:user, :manager)
@ability = Ability.new(@manager)
end
it "should not be able to create Questions" do
expect(@ability).not_to be_able_to(:create, Question.new)
end
it "should not be able to read Questions" do
expect(@ability).not_to be_able_to(:read, Question.new)
end
it "should not be able to update Questions" do
expect(@ability).not_to be_able_to(:update, Question.new)
end
it "should not be able to delete Questions" do
expect(@ability).not_to be_able_to(:destroy, Question.new)
end
end
Run Code Online (Sandbox Code Playgroud)
这清楚地表明,类型的用户manager不应该具有对Question模型的任何形式的访问.
是否有一种直接的方法可以在一个块中编写整个it块,只有一个块expect …