如果您的控制器操作如下所示:
respond_to do |format|
format.html { raise 'Unsupported' }
format.js # index.js.erb
end
Run Code Online (Sandbox Code Playgroud)
你的功能测试看起来像这样:
test "javascript response..." do
get :index
end
Run Code Online (Sandbox Code Playgroud)
它将执行respond_to块的HTML分支.
如果你试试这个:
test "javascript response..." do
get 'index.js'
end
Run Code Online (Sandbox Code Playgroud)
它执行视图(index.js.erb)而没有运行控制器动作!
有谁知道如何测试活动上Toast消息的外观?
我正在使用类似于OP在此问题上发布的代码来测试从一个活动到下一个活动的程序流程.我还希望能够测试特定活动的Toast消息.
我知道Karma是一个JavaScript测试运行器,它可以在真正的浏览器中运行测试.如果是这种情况,Selenium会提供什么样的测试覆盖范围以及Karma.
javascript selenium browser-automation functional-testing karma-runner
我有一个A需要经过测试的课程.以下是定义A:
public class A {
public void methodOne(int argument) {
//some operations
methodTwo(int argument);
//some operations
}
private void methodTwo(int argument) {
DateTime dateTime = new DateTime();
//use dateTime to perform some operations
}
}
Run Code Online (Sandbox Code Playgroud)
并且基于该dateTime值,一些数据将被操纵,从数据库中检索.对于此数据库,值通过JSON文件保留.
这使事情复杂化.我需要的dateTime是在测试时将其设置为某个特定日期.有没有办法可以使用mockito模拟局部变量的值?
我最近听说过单元测试的功能测试.
我知道单元测试从最原子的形式测试给定代码片段的每种可能性.但功能测试怎么样?
这听起来像只测试代码是否有效,但它是否像单元测试一样可靠?
我被告知有两个关于这个问题的学校.某些人更喜欢单元测试,其他人更喜欢功能测试.
有没有什么好的资源,链接,书籍,任何参考文献或者你们中的任何一个能够解释和完善我的主题的人?
谢谢!
我正在尝试创建一个管理员用户作为我的tests.py的一部分来检查持久性.
更新:tests.py是子类TestCase的标准格式,下面的代码在setUp()函数中调用.
我可以创建普通用户但不能创建管理员用户.如果我试试这个:
self.adminuser = User.objects.create_user('admin', 'admin@test.com', 'pass')
self.adminuser.save()
self.adminuser.is_staff = True
self.adminuser.save()
Run Code Online (Sandbox Code Playgroud)
或者self.adminuser = User.objects.create_superuser('admin','admin @ test.com','pass')self.adminuser.save()
我明白了:
Warning: Data truncated for column 'name' at row 1
Run Code Online (Sandbox Code Playgroud)
如果我删除is_staff行一切都很好(除了我不能做我的测试!)
我是否必须将管理员用户作为固定装置加载?
UserProfile定义如下:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
organisation = models.ForeignKey(Organisation, null=True, blank=True)
telephone = models.CharField(max_length=20, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
和完整错误回溯是:
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 242, in __call__
self._pre_setup()
File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 217, in _pre_setup
self._fixture_setup()
File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 440, in _fixture_setup
return super(TestCase, self)._fixture_setup()
File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 222, …Run Code Online (Sandbox Code Playgroud) 我的DoctrineFixturesBundle已安装,我可以通过命令行加载夹具但是,如何从功能测试中加载夹具?
嗨,我想对使用Devise和CanCan的Rails 3应用程序进行一些功能测试.
在我的用户模型中,我有用户年龄,我想测试用户只能访问某个页面,如果他们是:
我在Devise文档中看到我可以使用:*sign_in*并且我把它放在我的测试中它似乎工作 - 测试没有错误,因为我有:
include Devise::TestHelpers
Run Code Online (Sandbox Code Playgroud)
在我的*test_helper.rb*
当我拿出它时,我的测试会出错,因为*sign_in*没有定义.所以这不是帮手问题.
当我运行测试并检查span#loggedin是否有一次出现时,测试失败,因为有0次出现.span#loggedin仅出现*if user_signed_in?*
我需要在我的灯具或测试中创建一个也是完全注册用户(已确认等)的测试用户?
查看代码:
<% if user_signed_in? %>
<span id="loggedin">User is signed in</span>
User age is <span id="age"><%= current_user.age.to_s %></span>
<% end %>
Run Code Online (Sandbox Code Playgroud)
测试代码:
test "should get index" do
sign_in :one
get :index
assert_response :success
assert_select 'span#loggedin', :count => 1
end
Run Code Online (Sandbox Code Playgroud)
夹具:
one:
email: jez@example.com
age: 36
Run Code Online (Sandbox Code Playgroud)
当我手动登录时,它在开发中工作正常,但我希望自动化它 - 真正的测试点!
从这篇文章中获取问题(How to moq a Func)并对其进行了调整,因为答案是不正确的.
public class FooBar
{
private Func<IFooBarProxy> __fooBarProxyFactory;
public FooBar(Func<IFooBarProxy> fooBarProxyFactory)
{
_fooBarProxyFactory = fooBarProxyFactory;
}
public void Process()
{
_fooBarProxyFactory();
_fooBarProxyFactory();
}
}
Run Code Online (Sandbox Code Playgroud)
我需要模拟一个作为构造函数参数传递的Func <>,断言func被调用两次.
当试图模拟函数时var funcMock = new Mock<Func<IFooBarProxy>>();Moq引发异常,因为Func类型不可模拟.
问题是,如果没有模拟func,就不可能验证函数被调用(n)次. funcMock.Verify( (), Times.AtLeast(2));
我正在努力测试Rails中的更新方法.我正在使用标准的内置测试框架(test::unit)和Rails 3.0.8.我已经创建了一个最小的应用程序来测试它,但我无法让它工作.这是我做的:
我创建了一个新的空白rails应用程序:
rails new testapp
Run Code Online (Sandbox Code Playgroud)
创建一个名为Collection的模型:
rails generate model collection name:string
Run Code Online (Sandbox Code Playgroud)
运行rake db:migrate:
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
使用更新方法创建名为Collections的控制器:
rails generate controller collections update
Run Code Online (Sandbox Code Playgroud)
在collection_controller.rb中,我添加了这个最小更新方法:
def update
@collection = Collection.find(params[:id])
@collection.update_attributes(params[:collection])
end
Run Code Online (Sandbox Code Playgroud)
测试夹具是默认值(collections.yml):
one:
name: MyString
two:
name: MyString
Run Code Online (Sandbox Code Playgroud)
然后我将它添加到功能下的collection_controller_test.rb中:
test "should update collection" do
put :update, :id => collections(:one), :collection => {:name => 'MyString2'}
assert_equal "MyString2", collections(:one).name
end
Run Code Online (Sandbox Code Playgroud)
当我运行测试时:
rake test:functionals
Run Code Online (Sandbox Code Playgroud)
它失败了这条消息:
test_should_update_collection(CollectionsControllerTest) [/Users/atle/Documents/Rails/testapp/test/functional/collections_controller_test.rb:6]:
<"MyString2"> expected but was
<"MyString">.
Run Code Online (Sandbox Code Playgroud)
以下是test.log的输出:
[1m[36mSQL (0.2ms)[0m [1m SELECT name
FROM sqlite_master
WHERE type …Run Code Online (Sandbox Code Playgroud)